af_packet.c 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118
  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. spin_lock(&sk->sk_receive_queue.lock);
  817. po->stats.tp_drops++;
  818. atomic_inc(&sk->sk_drops);
  819. spin_unlock(&sk->sk_receive_queue.lock);
  820. drop_n_restore:
  821. if (skb_head != skb->data && skb_shared(skb)) {
  822. skb->data = skb_head;
  823. skb->len = skb_len;
  824. }
  825. drop:
  826. consume_skb(skb);
  827. return 0;
  828. }
  829. static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,
  830. struct packet_type *pt, struct net_device *orig_dev)
  831. {
  832. struct sock *sk;
  833. struct packet_sock *po;
  834. struct sockaddr_ll *sll;
  835. union {
  836. struct tpacket_hdr *h1;
  837. struct tpacket2_hdr *h2;
  838. void *raw;
  839. } h;
  840. u8 *skb_head = skb->data;
  841. int skb_len = skb->len;
  842. unsigned int snaplen, res;
  843. unsigned long status = TP_STATUS_LOSING|TP_STATUS_USER;
  844. unsigned short macoff, netoff, hdrlen;
  845. struct sk_buff *copy_skb = NULL;
  846. struct timeval tv;
  847. struct timespec ts;
  848. struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
  849. if (skb->pkt_type == PACKET_LOOPBACK)
  850. goto drop;
  851. sk = pt->af_packet_priv;
  852. po = pkt_sk(sk);
  853. if (!net_eq(dev_net(dev), sock_net(sk)))
  854. goto drop;
  855. if (dev->header_ops) {
  856. if (sk->sk_type != SOCK_DGRAM)
  857. skb_push(skb, skb->data - skb_mac_header(skb));
  858. else if (skb->pkt_type == PACKET_OUTGOING) {
  859. /* Special case: outgoing packets have ll header at head */
  860. skb_pull(skb, skb_network_offset(skb));
  861. }
  862. }
  863. if (skb->ip_summed == CHECKSUM_PARTIAL)
  864. status |= TP_STATUS_CSUMNOTREADY;
  865. snaplen = skb->len;
  866. res = run_filter(skb, sk, snaplen);
  867. if (!res)
  868. goto drop_n_restore;
  869. if (snaplen > res)
  870. snaplen = res;
  871. if (sk->sk_type == SOCK_DGRAM) {
  872. macoff = netoff = TPACKET_ALIGN(po->tp_hdrlen) + 16 +
  873. po->tp_reserve;
  874. } else {
  875. unsigned maclen = skb_network_offset(skb);
  876. netoff = TPACKET_ALIGN(po->tp_hdrlen +
  877. (maclen < 16 ? 16 : maclen)) +
  878. po->tp_reserve;
  879. macoff = netoff - maclen;
  880. }
  881. if (macoff + snaplen > po->rx_ring.frame_size) {
  882. if (po->copy_thresh &&
  883. atomic_read(&sk->sk_rmem_alloc) + skb->truesize <
  884. (unsigned)sk->sk_rcvbuf) {
  885. if (skb_shared(skb)) {
  886. copy_skb = skb_clone(skb, GFP_ATOMIC);
  887. } else {
  888. copy_skb = skb_get(skb);
  889. skb_head = skb->data;
  890. }
  891. if (copy_skb)
  892. skb_set_owner_r(copy_skb, sk);
  893. }
  894. snaplen = po->rx_ring.frame_size - macoff;
  895. if ((int)snaplen < 0)
  896. snaplen = 0;
  897. }
  898. spin_lock(&sk->sk_receive_queue.lock);
  899. h.raw = packet_current_frame(po, &po->rx_ring, TP_STATUS_KERNEL);
  900. if (!h.raw)
  901. goto ring_is_full;
  902. packet_increment_head(&po->rx_ring);
  903. po->stats.tp_packets++;
  904. if (copy_skb) {
  905. status |= TP_STATUS_COPY;
  906. __skb_queue_tail(&sk->sk_receive_queue, copy_skb);
  907. }
  908. if (!po->stats.tp_drops)
  909. status &= ~TP_STATUS_LOSING;
  910. spin_unlock(&sk->sk_receive_queue.lock);
  911. skb_copy_bits(skb, 0, h.raw + macoff, snaplen);
  912. switch (po->tp_version) {
  913. case TPACKET_V1:
  914. h.h1->tp_len = skb->len;
  915. h.h1->tp_snaplen = snaplen;
  916. h.h1->tp_mac = macoff;
  917. h.h1->tp_net = netoff;
  918. if ((po->tp_tstamp & SOF_TIMESTAMPING_SYS_HARDWARE)
  919. && shhwtstamps->syststamp.tv64)
  920. tv = ktime_to_timeval(shhwtstamps->syststamp);
  921. else if ((po->tp_tstamp & SOF_TIMESTAMPING_RAW_HARDWARE)
  922. && shhwtstamps->hwtstamp.tv64)
  923. tv = ktime_to_timeval(shhwtstamps->hwtstamp);
  924. else if (skb->tstamp.tv64)
  925. tv = ktime_to_timeval(skb->tstamp);
  926. else
  927. do_gettimeofday(&tv);
  928. h.h1->tp_sec = tv.tv_sec;
  929. h.h1->tp_usec = tv.tv_usec;
  930. hdrlen = sizeof(*h.h1);
  931. break;
  932. case TPACKET_V2:
  933. h.h2->tp_len = skb->len;
  934. h.h2->tp_snaplen = snaplen;
  935. h.h2->tp_mac = macoff;
  936. h.h2->tp_net = netoff;
  937. if ((po->tp_tstamp & SOF_TIMESTAMPING_SYS_HARDWARE)
  938. && shhwtstamps->syststamp.tv64)
  939. ts = ktime_to_timespec(shhwtstamps->syststamp);
  940. else if ((po->tp_tstamp & SOF_TIMESTAMPING_RAW_HARDWARE)
  941. && shhwtstamps->hwtstamp.tv64)
  942. ts = ktime_to_timespec(shhwtstamps->hwtstamp);
  943. else if (skb->tstamp.tv64)
  944. ts = ktime_to_timespec(skb->tstamp);
  945. else
  946. getnstimeofday(&ts);
  947. h.h2->tp_sec = ts.tv_sec;
  948. h.h2->tp_nsec = ts.tv_nsec;
  949. if (vlan_tx_tag_present(skb)) {
  950. h.h2->tp_vlan_tci = vlan_tx_tag_get(skb);
  951. status |= TP_STATUS_VLAN_VALID;
  952. } else {
  953. h.h2->tp_vlan_tci = 0;
  954. }
  955. h.h2->tp_padding = 0;
  956. hdrlen = sizeof(*h.h2);
  957. break;
  958. default:
  959. BUG();
  960. }
  961. sll = h.raw + TPACKET_ALIGN(hdrlen);
  962. sll->sll_halen = dev_parse_header(skb, sll->sll_addr);
  963. sll->sll_family = AF_PACKET;
  964. sll->sll_hatype = dev->type;
  965. sll->sll_protocol = skb->protocol;
  966. sll->sll_pkttype = skb->pkt_type;
  967. if (unlikely(po->origdev))
  968. sll->sll_ifindex = orig_dev->ifindex;
  969. else
  970. sll->sll_ifindex = dev->ifindex;
  971. smp_mb();
  972. #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1
  973. {
  974. u8 *start, *end;
  975. end = (u8 *)PAGE_ALIGN((unsigned long)h.raw + macoff + snaplen);
  976. for (start = h.raw; start < end; start += PAGE_SIZE)
  977. flush_dcache_page(pgv_to_page(start));
  978. smp_wmb();
  979. }
  980. #endif
  981. __packet_set_status(po, h.raw, status);
  982. sk->sk_data_ready(sk, 0);
  983. drop_n_restore:
  984. if (skb_head != skb->data && skb_shared(skb)) {
  985. skb->data = skb_head;
  986. skb->len = skb_len;
  987. }
  988. drop:
  989. kfree_skb(skb);
  990. return 0;
  991. ring_is_full:
  992. po->stats.tp_drops++;
  993. spin_unlock(&sk->sk_receive_queue.lock);
  994. sk->sk_data_ready(sk, 0);
  995. kfree_skb(copy_skb);
  996. goto drop_n_restore;
  997. }
  998. static void tpacket_destruct_skb(struct sk_buff *skb)
  999. {
  1000. struct packet_sock *po = pkt_sk(skb->sk);
  1001. void *ph;
  1002. BUG_ON(skb == NULL);
  1003. if (likely(po->tx_ring.pg_vec)) {
  1004. ph = skb_shinfo(skb)->destructor_arg;
  1005. BUG_ON(__packet_get_status(po, ph) != TP_STATUS_SENDING);
  1006. BUG_ON(atomic_read(&po->tx_ring.pending) == 0);
  1007. atomic_dec(&po->tx_ring.pending);
  1008. __packet_set_status(po, ph, TP_STATUS_AVAILABLE);
  1009. }
  1010. sock_wfree(skb);
  1011. }
  1012. static int tpacket_fill_skb(struct packet_sock *po, struct sk_buff *skb,
  1013. void *frame, struct net_device *dev, int size_max,
  1014. __be16 proto, unsigned char *addr)
  1015. {
  1016. union {
  1017. struct tpacket_hdr *h1;
  1018. struct tpacket2_hdr *h2;
  1019. void *raw;
  1020. } ph;
  1021. int to_write, offset, len, tp_len, nr_frags, len_max;
  1022. struct socket *sock = po->sk.sk_socket;
  1023. struct page *page;
  1024. void *data;
  1025. int err;
  1026. ph.raw = frame;
  1027. skb->protocol = proto;
  1028. skb->dev = dev;
  1029. skb->priority = po->sk.sk_priority;
  1030. skb->mark = po->sk.sk_mark;
  1031. skb_shinfo(skb)->destructor_arg = ph.raw;
  1032. switch (po->tp_version) {
  1033. case TPACKET_V2:
  1034. tp_len = ph.h2->tp_len;
  1035. break;
  1036. default:
  1037. tp_len = ph.h1->tp_len;
  1038. break;
  1039. }
  1040. if (unlikely(tp_len > size_max)) {
  1041. pr_err("packet size is too long (%d > %d)\n", tp_len, size_max);
  1042. return -EMSGSIZE;
  1043. }
  1044. skb_reserve(skb, LL_RESERVED_SPACE(dev));
  1045. skb_reset_network_header(skb);
  1046. data = ph.raw + po->tp_hdrlen - sizeof(struct sockaddr_ll);
  1047. to_write = tp_len;
  1048. if (sock->type == SOCK_DGRAM) {
  1049. err = dev_hard_header(skb, dev, ntohs(proto), addr,
  1050. NULL, tp_len);
  1051. if (unlikely(err < 0))
  1052. return -EINVAL;
  1053. } else if (dev->hard_header_len) {
  1054. /* net device doesn't like empty head */
  1055. if (unlikely(tp_len <= dev->hard_header_len)) {
  1056. pr_err("packet size is too short (%d < %d)\n",
  1057. tp_len, dev->hard_header_len);
  1058. return -EINVAL;
  1059. }
  1060. skb_push(skb, dev->hard_header_len);
  1061. err = skb_store_bits(skb, 0, data,
  1062. dev->hard_header_len);
  1063. if (unlikely(err))
  1064. return err;
  1065. data += dev->hard_header_len;
  1066. to_write -= dev->hard_header_len;
  1067. }
  1068. err = -EFAULT;
  1069. offset = offset_in_page(data);
  1070. len_max = PAGE_SIZE - offset;
  1071. len = ((to_write > len_max) ? len_max : to_write);
  1072. skb->data_len = to_write;
  1073. skb->len += to_write;
  1074. skb->truesize += to_write;
  1075. atomic_add(to_write, &po->sk.sk_wmem_alloc);
  1076. while (likely(to_write)) {
  1077. nr_frags = skb_shinfo(skb)->nr_frags;
  1078. if (unlikely(nr_frags >= MAX_SKB_FRAGS)) {
  1079. pr_err("Packet exceed the number of skb frags(%lu)\n",
  1080. MAX_SKB_FRAGS);
  1081. return -EFAULT;
  1082. }
  1083. page = pgv_to_page(data);
  1084. data += len;
  1085. flush_dcache_page(page);
  1086. get_page(page);
  1087. skb_fill_page_desc(skb, nr_frags, page, offset, len);
  1088. to_write -= len;
  1089. offset = 0;
  1090. len_max = PAGE_SIZE;
  1091. len = ((to_write > len_max) ? len_max : to_write);
  1092. }
  1093. return tp_len;
  1094. }
  1095. static int tpacket_snd(struct packet_sock *po, struct msghdr *msg)
  1096. {
  1097. struct sk_buff *skb;
  1098. struct net_device *dev;
  1099. __be16 proto;
  1100. bool need_rls_dev = false;
  1101. int err, reserve = 0;
  1102. void *ph;
  1103. struct sockaddr_ll *saddr = (struct sockaddr_ll *)msg->msg_name;
  1104. int tp_len, size_max;
  1105. unsigned char *addr;
  1106. int len_sum = 0;
  1107. int status = 0;
  1108. mutex_lock(&po->pg_vec_lock);
  1109. err = -EBUSY;
  1110. if (saddr == NULL) {
  1111. dev = po->prot_hook.dev;
  1112. proto = po->num;
  1113. addr = NULL;
  1114. } else {
  1115. err = -EINVAL;
  1116. if (msg->msg_namelen < sizeof(struct sockaddr_ll))
  1117. goto out;
  1118. if (msg->msg_namelen < (saddr->sll_halen
  1119. + offsetof(struct sockaddr_ll,
  1120. sll_addr)))
  1121. goto out;
  1122. proto = saddr->sll_protocol;
  1123. addr = saddr->sll_addr;
  1124. dev = dev_get_by_index(sock_net(&po->sk), saddr->sll_ifindex);
  1125. need_rls_dev = true;
  1126. }
  1127. err = -ENXIO;
  1128. if (unlikely(dev == NULL))
  1129. goto out;
  1130. reserve = dev->hard_header_len;
  1131. err = -ENETDOWN;
  1132. if (unlikely(!(dev->flags & IFF_UP)))
  1133. goto out_put;
  1134. size_max = po->tx_ring.frame_size
  1135. - (po->tp_hdrlen - sizeof(struct sockaddr_ll));
  1136. if (size_max > dev->mtu + reserve)
  1137. size_max = dev->mtu + reserve;
  1138. do {
  1139. ph = packet_current_frame(po, &po->tx_ring,
  1140. TP_STATUS_SEND_REQUEST);
  1141. if (unlikely(ph == NULL)) {
  1142. schedule();
  1143. continue;
  1144. }
  1145. status = TP_STATUS_SEND_REQUEST;
  1146. skb = sock_alloc_send_skb(&po->sk,
  1147. LL_ALLOCATED_SPACE(dev)
  1148. + sizeof(struct sockaddr_ll),
  1149. 0, &err);
  1150. if (unlikely(skb == NULL))
  1151. goto out_status;
  1152. tp_len = tpacket_fill_skb(po, skb, ph, dev, size_max, proto,
  1153. addr);
  1154. if (unlikely(tp_len < 0)) {
  1155. if (po->tp_loss) {
  1156. __packet_set_status(po, ph,
  1157. TP_STATUS_AVAILABLE);
  1158. packet_increment_head(&po->tx_ring);
  1159. kfree_skb(skb);
  1160. continue;
  1161. } else {
  1162. status = TP_STATUS_WRONG_FORMAT;
  1163. err = tp_len;
  1164. goto out_status;
  1165. }
  1166. }
  1167. skb->destructor = tpacket_destruct_skb;
  1168. __packet_set_status(po, ph, TP_STATUS_SENDING);
  1169. atomic_inc(&po->tx_ring.pending);
  1170. status = TP_STATUS_SEND_REQUEST;
  1171. err = dev_queue_xmit(skb);
  1172. if (unlikely(err > 0)) {
  1173. err = net_xmit_errno(err);
  1174. if (err && __packet_get_status(po, ph) ==
  1175. TP_STATUS_AVAILABLE) {
  1176. /* skb was destructed already */
  1177. skb = NULL;
  1178. goto out_status;
  1179. }
  1180. /*
  1181. * skb was dropped but not destructed yet;
  1182. * let's treat it like congestion or err < 0
  1183. */
  1184. err = 0;
  1185. }
  1186. packet_increment_head(&po->tx_ring);
  1187. len_sum += tp_len;
  1188. } while (likely((ph != NULL) ||
  1189. ((!(msg->msg_flags & MSG_DONTWAIT)) &&
  1190. (atomic_read(&po->tx_ring.pending))))
  1191. );
  1192. err = len_sum;
  1193. goto out_put;
  1194. out_status:
  1195. __packet_set_status(po, ph, status);
  1196. kfree_skb(skb);
  1197. out_put:
  1198. if (need_rls_dev)
  1199. dev_put(dev);
  1200. out:
  1201. mutex_unlock(&po->pg_vec_lock);
  1202. return err;
  1203. }
  1204. static inline struct sk_buff *packet_alloc_skb(struct sock *sk, size_t prepad,
  1205. size_t reserve, size_t len,
  1206. size_t linear, int noblock,
  1207. int *err)
  1208. {
  1209. struct sk_buff *skb;
  1210. /* Under a page? Don't bother with paged skb. */
  1211. if (prepad + len < PAGE_SIZE || !linear)
  1212. linear = len;
  1213. skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
  1214. err);
  1215. if (!skb)
  1216. return NULL;
  1217. skb_reserve(skb, reserve);
  1218. skb_put(skb, linear);
  1219. skb->data_len = len - linear;
  1220. skb->len += len - linear;
  1221. return skb;
  1222. }
  1223. static int packet_snd(struct socket *sock,
  1224. struct msghdr *msg, size_t len)
  1225. {
  1226. struct sock *sk = sock->sk;
  1227. struct sockaddr_ll *saddr = (struct sockaddr_ll *)msg->msg_name;
  1228. struct sk_buff *skb;
  1229. struct net_device *dev;
  1230. __be16 proto;
  1231. bool need_rls_dev = false;
  1232. unsigned char *addr;
  1233. int err, reserve = 0;
  1234. struct virtio_net_hdr vnet_hdr = { 0 };
  1235. int offset = 0;
  1236. int vnet_hdr_len;
  1237. struct packet_sock *po = pkt_sk(sk);
  1238. unsigned short gso_type = 0;
  1239. /*
  1240. * Get and verify the address.
  1241. */
  1242. if (saddr == NULL) {
  1243. dev = po->prot_hook.dev;
  1244. proto = po->num;
  1245. addr = NULL;
  1246. } else {
  1247. err = -EINVAL;
  1248. if (msg->msg_namelen < sizeof(struct sockaddr_ll))
  1249. goto out;
  1250. if (msg->msg_namelen < (saddr->sll_halen + offsetof(struct sockaddr_ll, sll_addr)))
  1251. goto out;
  1252. proto = saddr->sll_protocol;
  1253. addr = saddr->sll_addr;
  1254. dev = dev_get_by_index(sock_net(sk), saddr->sll_ifindex);
  1255. need_rls_dev = true;
  1256. }
  1257. err = -ENXIO;
  1258. if (dev == NULL)
  1259. goto out_unlock;
  1260. if (sock->type == SOCK_RAW)
  1261. reserve = dev->hard_header_len;
  1262. err = -ENETDOWN;
  1263. if (!(dev->flags & IFF_UP))
  1264. goto out_unlock;
  1265. if (po->has_vnet_hdr) {
  1266. vnet_hdr_len = sizeof(vnet_hdr);
  1267. err = -EINVAL;
  1268. if (len < vnet_hdr_len)
  1269. goto out_unlock;
  1270. len -= vnet_hdr_len;
  1271. err = memcpy_fromiovec((void *)&vnet_hdr, msg->msg_iov,
  1272. vnet_hdr_len);
  1273. if (err < 0)
  1274. goto out_unlock;
  1275. if ((vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
  1276. (vnet_hdr.csum_start + vnet_hdr.csum_offset + 2 >
  1277. vnet_hdr.hdr_len))
  1278. vnet_hdr.hdr_len = vnet_hdr.csum_start +
  1279. vnet_hdr.csum_offset + 2;
  1280. err = -EINVAL;
  1281. if (vnet_hdr.hdr_len > len)
  1282. goto out_unlock;
  1283. if (vnet_hdr.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
  1284. switch (vnet_hdr.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
  1285. case VIRTIO_NET_HDR_GSO_TCPV4:
  1286. gso_type = SKB_GSO_TCPV4;
  1287. break;
  1288. case VIRTIO_NET_HDR_GSO_TCPV6:
  1289. gso_type = SKB_GSO_TCPV6;
  1290. break;
  1291. case VIRTIO_NET_HDR_GSO_UDP:
  1292. gso_type = SKB_GSO_UDP;
  1293. break;
  1294. default:
  1295. goto out_unlock;
  1296. }
  1297. if (vnet_hdr.gso_type & VIRTIO_NET_HDR_GSO_ECN)
  1298. gso_type |= SKB_GSO_TCP_ECN;
  1299. if (vnet_hdr.gso_size == 0)
  1300. goto out_unlock;
  1301. }
  1302. }
  1303. err = -EMSGSIZE;
  1304. if (!gso_type && (len > dev->mtu + reserve + VLAN_HLEN))
  1305. goto out_unlock;
  1306. err = -ENOBUFS;
  1307. skb = packet_alloc_skb(sk, LL_ALLOCATED_SPACE(dev),
  1308. LL_RESERVED_SPACE(dev), len, vnet_hdr.hdr_len,
  1309. msg->msg_flags & MSG_DONTWAIT, &err);
  1310. if (skb == NULL)
  1311. goto out_unlock;
  1312. skb_set_network_header(skb, reserve);
  1313. err = -EINVAL;
  1314. if (sock->type == SOCK_DGRAM &&
  1315. (offset = dev_hard_header(skb, dev, ntohs(proto), addr, NULL, len)) < 0)
  1316. goto out_free;
  1317. /* Returns -EFAULT on error */
  1318. err = skb_copy_datagram_from_iovec(skb, offset, msg->msg_iov, 0, len);
  1319. if (err)
  1320. goto out_free;
  1321. err = sock_tx_timestamp(sk, &skb_shinfo(skb)->tx_flags);
  1322. if (err < 0)
  1323. goto out_free;
  1324. if (!gso_type && (len > dev->mtu + reserve)) {
  1325. /* Earlier code assumed this would be a VLAN pkt,
  1326. * double-check this now that we have the actual
  1327. * packet in hand.
  1328. */
  1329. struct ethhdr *ehdr;
  1330. skb_reset_mac_header(skb);
  1331. ehdr = eth_hdr(skb);
  1332. if (ehdr->h_proto != htons(ETH_P_8021Q)) {
  1333. err = -EMSGSIZE;
  1334. goto out_free;
  1335. }
  1336. }
  1337. skb->protocol = proto;
  1338. skb->dev = dev;
  1339. skb->priority = sk->sk_priority;
  1340. skb->mark = sk->sk_mark;
  1341. if (po->has_vnet_hdr) {
  1342. if (vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
  1343. if (!skb_partial_csum_set(skb, vnet_hdr.csum_start,
  1344. vnet_hdr.csum_offset)) {
  1345. err = -EINVAL;
  1346. goto out_free;
  1347. }
  1348. }
  1349. skb_shinfo(skb)->gso_size = vnet_hdr.gso_size;
  1350. skb_shinfo(skb)->gso_type = gso_type;
  1351. /* Header must be checked, and gso_segs computed. */
  1352. skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
  1353. skb_shinfo(skb)->gso_segs = 0;
  1354. len += vnet_hdr_len;
  1355. }
  1356. /*
  1357. * Now send it
  1358. */
  1359. err = dev_queue_xmit(skb);
  1360. if (err > 0 && (err = net_xmit_errno(err)) != 0)
  1361. goto out_unlock;
  1362. if (need_rls_dev)
  1363. dev_put(dev);
  1364. return len;
  1365. out_free:
  1366. kfree_skb(skb);
  1367. out_unlock:
  1368. if (dev && need_rls_dev)
  1369. dev_put(dev);
  1370. out:
  1371. return err;
  1372. }
  1373. static int packet_sendmsg(struct kiocb *iocb, struct socket *sock,
  1374. struct msghdr *msg, size_t len)
  1375. {
  1376. struct sock *sk = sock->sk;
  1377. struct packet_sock *po = pkt_sk(sk);
  1378. if (po->tx_ring.pg_vec)
  1379. return tpacket_snd(po, msg);
  1380. else
  1381. return packet_snd(sock, msg, len);
  1382. }
  1383. /*
  1384. * Close a PACKET socket. This is fairly simple. We immediately go
  1385. * to 'closed' state and remove our protocol entry in the device list.
  1386. */
  1387. static int packet_release(struct socket *sock)
  1388. {
  1389. struct sock *sk = sock->sk;
  1390. struct packet_sock *po;
  1391. struct net *net;
  1392. struct tpacket_req req;
  1393. if (!sk)
  1394. return 0;
  1395. net = sock_net(sk);
  1396. po = pkt_sk(sk);
  1397. spin_lock_bh(&net->packet.sklist_lock);
  1398. sk_del_node_init_rcu(sk);
  1399. sock_prot_inuse_add(net, sk->sk_prot, -1);
  1400. spin_unlock_bh(&net->packet.sklist_lock);
  1401. spin_lock(&po->bind_lock);
  1402. unregister_prot_hook(sk, false);
  1403. if (po->prot_hook.dev) {
  1404. dev_put(po->prot_hook.dev);
  1405. po->prot_hook.dev = NULL;
  1406. }
  1407. spin_unlock(&po->bind_lock);
  1408. packet_flush_mclist(sk);
  1409. memset(&req, 0, sizeof(req));
  1410. if (po->rx_ring.pg_vec)
  1411. packet_set_ring(sk, &req, 1, 0);
  1412. if (po->tx_ring.pg_vec)
  1413. packet_set_ring(sk, &req, 1, 1);
  1414. fanout_release(sk);
  1415. synchronize_net();
  1416. /*
  1417. * Now the socket is dead. No more input will appear.
  1418. */
  1419. sock_orphan(sk);
  1420. sock->sk = NULL;
  1421. /* Purge queues */
  1422. skb_queue_purge(&sk->sk_receive_queue);
  1423. sk_refcnt_debug_release(sk);
  1424. sock_put(sk);
  1425. return 0;
  1426. }
  1427. /*
  1428. * Attach a packet hook.
  1429. */
  1430. static int packet_do_bind(struct sock *sk, struct net_device *dev, __be16 protocol)
  1431. {
  1432. struct packet_sock *po = pkt_sk(sk);
  1433. if (po->fanout)
  1434. return -EINVAL;
  1435. lock_sock(sk);
  1436. spin_lock(&po->bind_lock);
  1437. unregister_prot_hook(sk, true);
  1438. po->num = protocol;
  1439. po->prot_hook.type = protocol;
  1440. if (po->prot_hook.dev)
  1441. dev_put(po->prot_hook.dev);
  1442. po->prot_hook.dev = dev;
  1443. po->ifindex = dev ? dev->ifindex : 0;
  1444. if (protocol == 0)
  1445. goto out_unlock;
  1446. if (!dev || (dev->flags & IFF_UP)) {
  1447. register_prot_hook(sk);
  1448. } else {
  1449. sk->sk_err = ENETDOWN;
  1450. if (!sock_flag(sk, SOCK_DEAD))
  1451. sk->sk_error_report(sk);
  1452. }
  1453. out_unlock:
  1454. spin_unlock(&po->bind_lock);
  1455. release_sock(sk);
  1456. return 0;
  1457. }
  1458. /*
  1459. * Bind a packet socket to a device
  1460. */
  1461. static int packet_bind_spkt(struct socket *sock, struct sockaddr *uaddr,
  1462. int addr_len)
  1463. {
  1464. struct sock *sk = sock->sk;
  1465. char name[15];
  1466. struct net_device *dev;
  1467. int err = -ENODEV;
  1468. /*
  1469. * Check legality
  1470. */
  1471. if (addr_len != sizeof(struct sockaddr))
  1472. return -EINVAL;
  1473. strlcpy(name, uaddr->sa_data, sizeof(name));
  1474. dev = dev_get_by_name(sock_net(sk), name);
  1475. if (dev)
  1476. err = packet_do_bind(sk, dev, pkt_sk(sk)->num);
  1477. return err;
  1478. }
  1479. static int packet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
  1480. {
  1481. struct sockaddr_ll *sll = (struct sockaddr_ll *)uaddr;
  1482. struct sock *sk = sock->sk;
  1483. struct net_device *dev = NULL;
  1484. int err;
  1485. /*
  1486. * Check legality
  1487. */
  1488. if (addr_len < sizeof(struct sockaddr_ll))
  1489. return -EINVAL;
  1490. if (sll->sll_family != AF_PACKET)
  1491. return -EINVAL;
  1492. if (sll->sll_ifindex) {
  1493. err = -ENODEV;
  1494. dev = dev_get_by_index(sock_net(sk), sll->sll_ifindex);
  1495. if (dev == NULL)
  1496. goto out;
  1497. }
  1498. err = packet_do_bind(sk, dev, sll->sll_protocol ? : pkt_sk(sk)->num);
  1499. out:
  1500. return err;
  1501. }
  1502. static struct proto packet_proto = {
  1503. .name = "PACKET",
  1504. .owner = THIS_MODULE,
  1505. .obj_size = sizeof(struct packet_sock),
  1506. };
  1507. /*
  1508. * Create a packet of type SOCK_PACKET.
  1509. */
  1510. static int packet_create(struct net *net, struct socket *sock, int protocol,
  1511. int kern)
  1512. {
  1513. struct sock *sk;
  1514. struct packet_sock *po;
  1515. __be16 proto = (__force __be16)protocol; /* weird, but documented */
  1516. int err;
  1517. if (!capable(CAP_NET_RAW))
  1518. return -EPERM;
  1519. if (sock->type != SOCK_DGRAM && sock->type != SOCK_RAW &&
  1520. sock->type != SOCK_PACKET)
  1521. return -ESOCKTNOSUPPORT;
  1522. sock->state = SS_UNCONNECTED;
  1523. err = -ENOBUFS;
  1524. sk = sk_alloc(net, PF_PACKET, GFP_KERNEL, &packet_proto);
  1525. if (sk == NULL)
  1526. goto out;
  1527. sock->ops = &packet_ops;
  1528. if (sock->type == SOCK_PACKET)
  1529. sock->ops = &packet_ops_spkt;
  1530. sock_init_data(sock, sk);
  1531. po = pkt_sk(sk);
  1532. sk->sk_family = PF_PACKET;
  1533. po->num = proto;
  1534. sk->sk_destruct = packet_sock_destruct;
  1535. sk_refcnt_debug_inc(sk);
  1536. /*
  1537. * Attach a protocol block
  1538. */
  1539. spin_lock_init(&po->bind_lock);
  1540. mutex_init(&po->pg_vec_lock);
  1541. po->prot_hook.func = packet_rcv;
  1542. if (sock->type == SOCK_PACKET)
  1543. po->prot_hook.func = packet_rcv_spkt;
  1544. po->prot_hook.af_packet_priv = sk;
  1545. if (proto) {
  1546. po->prot_hook.type = proto;
  1547. register_prot_hook(sk);
  1548. }
  1549. spin_lock_bh(&net->packet.sklist_lock);
  1550. sk_add_node_rcu(sk, &net->packet.sklist);
  1551. sock_prot_inuse_add(net, &packet_proto, 1);
  1552. spin_unlock_bh(&net->packet.sklist_lock);
  1553. return 0;
  1554. out:
  1555. return err;
  1556. }
  1557. static int packet_recv_error(struct sock *sk, struct msghdr *msg, int len)
  1558. {
  1559. struct sock_exterr_skb *serr;
  1560. struct sk_buff *skb, *skb2;
  1561. int copied, err;
  1562. err = -EAGAIN;
  1563. skb = skb_dequeue(&sk->sk_error_queue);
  1564. if (skb == NULL)
  1565. goto out;
  1566. copied = skb->len;
  1567. if (copied > len) {
  1568. msg->msg_flags |= MSG_TRUNC;
  1569. copied = len;
  1570. }
  1571. err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
  1572. if (err)
  1573. goto out_free_skb;
  1574. sock_recv_timestamp(msg, sk, skb);
  1575. serr = SKB_EXT_ERR(skb);
  1576. put_cmsg(msg, SOL_PACKET, PACKET_TX_TIMESTAMP,
  1577. sizeof(serr->ee), &serr->ee);
  1578. msg->msg_flags |= MSG_ERRQUEUE;
  1579. err = copied;
  1580. /* Reset and regenerate socket error */
  1581. spin_lock_bh(&sk->sk_error_queue.lock);
  1582. sk->sk_err = 0;
  1583. if ((skb2 = skb_peek(&sk->sk_error_queue)) != NULL) {
  1584. sk->sk_err = SKB_EXT_ERR(skb2)->ee.ee_errno;
  1585. spin_unlock_bh(&sk->sk_error_queue.lock);
  1586. sk->sk_error_report(sk);
  1587. } else
  1588. spin_unlock_bh(&sk->sk_error_queue.lock);
  1589. out_free_skb:
  1590. kfree_skb(skb);
  1591. out:
  1592. return err;
  1593. }
  1594. /*
  1595. * Pull a packet from our receive queue and hand it to the user.
  1596. * If necessary we block.
  1597. */
  1598. static int packet_recvmsg(struct kiocb *iocb, struct socket *sock,
  1599. struct msghdr *msg, size_t len, int flags)
  1600. {
  1601. struct sock *sk = sock->sk;
  1602. struct sk_buff *skb;
  1603. int copied, err;
  1604. struct sockaddr_ll *sll;
  1605. int vnet_hdr_len = 0;
  1606. err = -EINVAL;
  1607. if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT|MSG_ERRQUEUE))
  1608. goto out;
  1609. #if 0
  1610. /* What error should we return now? EUNATTACH? */
  1611. if (pkt_sk(sk)->ifindex < 0)
  1612. return -ENODEV;
  1613. #endif
  1614. if (flags & MSG_ERRQUEUE) {
  1615. err = packet_recv_error(sk, msg, len);
  1616. goto out;
  1617. }
  1618. /*
  1619. * Call the generic datagram receiver. This handles all sorts
  1620. * of horrible races and re-entrancy so we can forget about it
  1621. * in the protocol layers.
  1622. *
  1623. * Now it will return ENETDOWN, if device have just gone down,
  1624. * but then it will block.
  1625. */
  1626. skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
  1627. /*
  1628. * An error occurred so return it. Because skb_recv_datagram()
  1629. * handles the blocking we don't see and worry about blocking
  1630. * retries.
  1631. */
  1632. if (skb == NULL)
  1633. goto out;
  1634. if (pkt_sk(sk)->has_vnet_hdr) {
  1635. struct virtio_net_hdr vnet_hdr = { 0 };
  1636. err = -EINVAL;
  1637. vnet_hdr_len = sizeof(vnet_hdr);
  1638. if (len < vnet_hdr_len)
  1639. goto out_free;
  1640. len -= vnet_hdr_len;
  1641. if (skb_is_gso(skb)) {
  1642. struct skb_shared_info *sinfo = skb_shinfo(skb);
  1643. /* This is a hint as to how much should be linear. */
  1644. vnet_hdr.hdr_len = skb_headlen(skb);
  1645. vnet_hdr.gso_size = sinfo->gso_size;
  1646. if (sinfo->gso_type & SKB_GSO_TCPV4)
  1647. vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
  1648. else if (sinfo->gso_type & SKB_GSO_TCPV6)
  1649. vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
  1650. else if (sinfo->gso_type & SKB_GSO_UDP)
  1651. vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_UDP;
  1652. else if (sinfo->gso_type & SKB_GSO_FCOE)
  1653. goto out_free;
  1654. else
  1655. BUG();
  1656. if (sinfo->gso_type & SKB_GSO_TCP_ECN)
  1657. vnet_hdr.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
  1658. } else
  1659. vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE;
  1660. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  1661. vnet_hdr.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
  1662. vnet_hdr.csum_start = skb_checksum_start_offset(skb);
  1663. vnet_hdr.csum_offset = skb->csum_offset;
  1664. } else if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
  1665. vnet_hdr.flags = VIRTIO_NET_HDR_F_DATA_VALID;
  1666. } /* else everything is zero */
  1667. err = memcpy_toiovec(msg->msg_iov, (void *)&vnet_hdr,
  1668. vnet_hdr_len);
  1669. if (err < 0)
  1670. goto out_free;
  1671. }
  1672. /*
  1673. * If the address length field is there to be filled in, we fill
  1674. * it in now.
  1675. */
  1676. sll = &PACKET_SKB_CB(skb)->sa.ll;
  1677. if (sock->type == SOCK_PACKET)
  1678. msg->msg_namelen = sizeof(struct sockaddr_pkt);
  1679. else
  1680. msg->msg_namelen = sll->sll_halen + offsetof(struct sockaddr_ll, sll_addr);
  1681. /*
  1682. * You lose any data beyond the buffer you gave. If it worries a
  1683. * user program they can ask the device for its MTU anyway.
  1684. */
  1685. copied = skb->len;
  1686. if (copied > len) {
  1687. copied = len;
  1688. msg->msg_flags |= MSG_TRUNC;
  1689. }
  1690. err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
  1691. if (err)
  1692. goto out_free;
  1693. sock_recv_ts_and_drops(msg, sk, skb);
  1694. if (msg->msg_name)
  1695. memcpy(msg->msg_name, &PACKET_SKB_CB(skb)->sa,
  1696. msg->msg_namelen);
  1697. if (pkt_sk(sk)->auxdata) {
  1698. struct tpacket_auxdata aux;
  1699. aux.tp_status = TP_STATUS_USER;
  1700. if (skb->ip_summed == CHECKSUM_PARTIAL)
  1701. aux.tp_status |= TP_STATUS_CSUMNOTREADY;
  1702. aux.tp_len = PACKET_SKB_CB(skb)->origlen;
  1703. aux.tp_snaplen = skb->len;
  1704. aux.tp_mac = 0;
  1705. aux.tp_net = skb_network_offset(skb);
  1706. if (vlan_tx_tag_present(skb)) {
  1707. aux.tp_vlan_tci = vlan_tx_tag_get(skb);
  1708. aux.tp_status |= TP_STATUS_VLAN_VALID;
  1709. } else {
  1710. aux.tp_vlan_tci = 0;
  1711. }
  1712. aux.tp_padding = 0;
  1713. put_cmsg(msg, SOL_PACKET, PACKET_AUXDATA, sizeof(aux), &aux);
  1714. }
  1715. /*
  1716. * Free or return the buffer as appropriate. Again this
  1717. * hides all the races and re-entrancy issues from us.
  1718. */
  1719. err = vnet_hdr_len + ((flags&MSG_TRUNC) ? skb->len : copied);
  1720. out_free:
  1721. skb_free_datagram(sk, skb);
  1722. out:
  1723. return err;
  1724. }
  1725. static int packet_getname_spkt(struct socket *sock, struct sockaddr *uaddr,
  1726. int *uaddr_len, int peer)
  1727. {
  1728. struct net_device *dev;
  1729. struct sock *sk = sock->sk;
  1730. if (peer)
  1731. return -EOPNOTSUPP;
  1732. uaddr->sa_family = AF_PACKET;
  1733. rcu_read_lock();
  1734. dev = dev_get_by_index_rcu(sock_net(sk), pkt_sk(sk)->ifindex);
  1735. if (dev)
  1736. strncpy(uaddr->sa_data, dev->name, 14);
  1737. else
  1738. memset(uaddr->sa_data, 0, 14);
  1739. rcu_read_unlock();
  1740. *uaddr_len = sizeof(*uaddr);
  1741. return 0;
  1742. }
  1743. static int packet_getname(struct socket *sock, struct sockaddr *uaddr,
  1744. int *uaddr_len, int peer)
  1745. {
  1746. struct net_device *dev;
  1747. struct sock *sk = sock->sk;
  1748. struct packet_sock *po = pkt_sk(sk);
  1749. DECLARE_SOCKADDR(struct sockaddr_ll *, sll, uaddr);
  1750. if (peer)
  1751. return -EOPNOTSUPP;
  1752. sll->sll_family = AF_PACKET;
  1753. sll->sll_ifindex = po->ifindex;
  1754. sll->sll_protocol = po->num;
  1755. sll->sll_pkttype = 0;
  1756. rcu_read_lock();
  1757. dev = dev_get_by_index_rcu(sock_net(sk), po->ifindex);
  1758. if (dev) {
  1759. sll->sll_hatype = dev->type;
  1760. sll->sll_halen = dev->addr_len;
  1761. memcpy(sll->sll_addr, dev->dev_addr, dev->addr_len);
  1762. } else {
  1763. sll->sll_hatype = 0; /* Bad: we have no ARPHRD_UNSPEC */
  1764. sll->sll_halen = 0;
  1765. }
  1766. rcu_read_unlock();
  1767. *uaddr_len = offsetof(struct sockaddr_ll, sll_addr) + sll->sll_halen;
  1768. return 0;
  1769. }
  1770. static int packet_dev_mc(struct net_device *dev, struct packet_mclist *i,
  1771. int what)
  1772. {
  1773. switch (i->type) {
  1774. case PACKET_MR_MULTICAST:
  1775. if (i->alen != dev->addr_len)
  1776. return -EINVAL;
  1777. if (what > 0)
  1778. return dev_mc_add(dev, i->addr);
  1779. else
  1780. return dev_mc_del(dev, i->addr);
  1781. break;
  1782. case PACKET_MR_PROMISC:
  1783. return dev_set_promiscuity(dev, what);
  1784. break;
  1785. case PACKET_MR_ALLMULTI:
  1786. return dev_set_allmulti(dev, what);
  1787. break;
  1788. case PACKET_MR_UNICAST:
  1789. if (i->alen != dev->addr_len)
  1790. return -EINVAL;
  1791. if (what > 0)
  1792. return dev_uc_add(dev, i->addr);
  1793. else
  1794. return dev_uc_del(dev, i->addr);
  1795. break;
  1796. default:
  1797. break;
  1798. }
  1799. return 0;
  1800. }
  1801. static void packet_dev_mclist(struct net_device *dev, struct packet_mclist *i, int what)
  1802. {
  1803. for ( ; i; i = i->next) {
  1804. if (i->ifindex == dev->ifindex)
  1805. packet_dev_mc(dev, i, what);
  1806. }
  1807. }
  1808. static int packet_mc_add(struct sock *sk, struct packet_mreq_max *mreq)
  1809. {
  1810. struct packet_sock *po = pkt_sk(sk);
  1811. struct packet_mclist *ml, *i;
  1812. struct net_device *dev;
  1813. int err;
  1814. rtnl_lock();
  1815. err = -ENODEV;
  1816. dev = __dev_get_by_index(sock_net(sk), mreq->mr_ifindex);
  1817. if (!dev)
  1818. goto done;
  1819. err = -EINVAL;
  1820. if (mreq->mr_alen > dev->addr_len)
  1821. goto done;
  1822. err = -ENOBUFS;
  1823. i = kmalloc(sizeof(*i), GFP_KERNEL);
  1824. if (i == NULL)
  1825. goto done;
  1826. err = 0;
  1827. for (ml = po->mclist; ml; ml = ml->next) {
  1828. if (ml->ifindex == mreq->mr_ifindex &&
  1829. ml->type == mreq->mr_type &&
  1830. ml->alen == mreq->mr_alen &&
  1831. memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) {
  1832. ml->count++;
  1833. /* Free the new element ... */
  1834. kfree(i);
  1835. goto done;
  1836. }
  1837. }
  1838. i->type = mreq->mr_type;
  1839. i->ifindex = mreq->mr_ifindex;
  1840. i->alen = mreq->mr_alen;
  1841. memcpy(i->addr, mreq->mr_address, i->alen);
  1842. i->count = 1;
  1843. i->next = po->mclist;
  1844. po->mclist = i;
  1845. err = packet_dev_mc(dev, i, 1);
  1846. if (err) {
  1847. po->mclist = i->next;
  1848. kfree(i);
  1849. }
  1850. done:
  1851. rtnl_unlock();
  1852. return err;
  1853. }
  1854. static int packet_mc_drop(struct sock *sk, struct packet_mreq_max *mreq)
  1855. {
  1856. struct packet_mclist *ml, **mlp;
  1857. rtnl_lock();
  1858. for (mlp = &pkt_sk(sk)->mclist; (ml = *mlp) != NULL; mlp = &ml->next) {
  1859. if (ml->ifindex == mreq->mr_ifindex &&
  1860. ml->type == mreq->mr_type &&
  1861. ml->alen == mreq->mr_alen &&
  1862. memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) {
  1863. if (--ml->count == 0) {
  1864. struct net_device *dev;
  1865. *mlp = ml->next;
  1866. dev = __dev_get_by_index(sock_net(sk), ml->ifindex);
  1867. if (dev)
  1868. packet_dev_mc(dev, ml, -1);
  1869. kfree(ml);
  1870. }
  1871. rtnl_unlock();
  1872. return 0;
  1873. }
  1874. }
  1875. rtnl_unlock();
  1876. return -EADDRNOTAVAIL;
  1877. }
  1878. static void packet_flush_mclist(struct sock *sk)
  1879. {
  1880. struct packet_sock *po = pkt_sk(sk);
  1881. struct packet_mclist *ml;
  1882. if (!po->mclist)
  1883. return;
  1884. rtnl_lock();
  1885. while ((ml = po->mclist) != NULL) {
  1886. struct net_device *dev;
  1887. po->mclist = ml->next;
  1888. dev = __dev_get_by_index(sock_net(sk), ml->ifindex);
  1889. if (dev != NULL)
  1890. packet_dev_mc(dev, ml, -1);
  1891. kfree(ml);
  1892. }
  1893. rtnl_unlock();
  1894. }
  1895. static int
  1896. packet_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen)
  1897. {
  1898. struct sock *sk = sock->sk;
  1899. struct packet_sock *po = pkt_sk(sk);
  1900. int ret;
  1901. if (level != SOL_PACKET)
  1902. return -ENOPROTOOPT;
  1903. switch (optname) {
  1904. case PACKET_ADD_MEMBERSHIP:
  1905. case PACKET_DROP_MEMBERSHIP:
  1906. {
  1907. struct packet_mreq_max mreq;
  1908. int len = optlen;
  1909. memset(&mreq, 0, sizeof(mreq));
  1910. if (len < sizeof(struct packet_mreq))
  1911. return -EINVAL;
  1912. if (len > sizeof(mreq))
  1913. len = sizeof(mreq);
  1914. if (copy_from_user(&mreq, optval, len))
  1915. return -EFAULT;
  1916. if (len < (mreq.mr_alen + offsetof(struct packet_mreq, mr_address)))
  1917. return -EINVAL;
  1918. if (optname == PACKET_ADD_MEMBERSHIP)
  1919. ret = packet_mc_add(sk, &mreq);
  1920. else
  1921. ret = packet_mc_drop(sk, &mreq);
  1922. return ret;
  1923. }
  1924. case PACKET_RX_RING:
  1925. case PACKET_TX_RING:
  1926. {
  1927. struct tpacket_req req;
  1928. if (optlen < sizeof(req))
  1929. return -EINVAL;
  1930. if (pkt_sk(sk)->has_vnet_hdr)
  1931. return -EINVAL;
  1932. if (copy_from_user(&req, optval, sizeof(req)))
  1933. return -EFAULT;
  1934. return packet_set_ring(sk, &req, 0, optname == PACKET_TX_RING);
  1935. }
  1936. case PACKET_COPY_THRESH:
  1937. {
  1938. int val;
  1939. if (optlen != sizeof(val))
  1940. return -EINVAL;
  1941. if (copy_from_user(&val, optval, sizeof(val)))
  1942. return -EFAULT;
  1943. pkt_sk(sk)->copy_thresh = val;
  1944. return 0;
  1945. }
  1946. case PACKET_VERSION:
  1947. {
  1948. int val;
  1949. if (optlen != sizeof(val))
  1950. return -EINVAL;
  1951. if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
  1952. return -EBUSY;
  1953. if (copy_from_user(&val, optval, sizeof(val)))
  1954. return -EFAULT;
  1955. switch (val) {
  1956. case TPACKET_V1:
  1957. case TPACKET_V2:
  1958. po->tp_version = val;
  1959. return 0;
  1960. default:
  1961. return -EINVAL;
  1962. }
  1963. }
  1964. case PACKET_RESERVE:
  1965. {
  1966. unsigned int val;
  1967. if (optlen != sizeof(val))
  1968. return -EINVAL;
  1969. if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
  1970. return -EBUSY;
  1971. if (copy_from_user(&val, optval, sizeof(val)))
  1972. return -EFAULT;
  1973. po->tp_reserve = val;
  1974. return 0;
  1975. }
  1976. case PACKET_LOSS:
  1977. {
  1978. unsigned int val;
  1979. if (optlen != sizeof(val))
  1980. return -EINVAL;
  1981. if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
  1982. return -EBUSY;
  1983. if (copy_from_user(&val, optval, sizeof(val)))
  1984. return -EFAULT;
  1985. po->tp_loss = !!val;
  1986. return 0;
  1987. }
  1988. case PACKET_AUXDATA:
  1989. {
  1990. int val;
  1991. if (optlen < sizeof(val))
  1992. return -EINVAL;
  1993. if (copy_from_user(&val, optval, sizeof(val)))
  1994. return -EFAULT;
  1995. po->auxdata = !!val;
  1996. return 0;
  1997. }
  1998. case PACKET_ORIGDEV:
  1999. {
  2000. int val;
  2001. if (optlen < sizeof(val))
  2002. return -EINVAL;
  2003. if (copy_from_user(&val, optval, sizeof(val)))
  2004. return -EFAULT;
  2005. po->origdev = !!val;
  2006. return 0;
  2007. }
  2008. case PACKET_VNET_HDR:
  2009. {
  2010. int val;
  2011. if (sock->type != SOCK_RAW)
  2012. return -EINVAL;
  2013. if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
  2014. return -EBUSY;
  2015. if (optlen < sizeof(val))
  2016. return -EINVAL;
  2017. if (copy_from_user(&val, optval, sizeof(val)))
  2018. return -EFAULT;
  2019. po->has_vnet_hdr = !!val;
  2020. return 0;
  2021. }
  2022. case PACKET_TIMESTAMP:
  2023. {
  2024. int val;
  2025. if (optlen != sizeof(val))
  2026. return -EINVAL;
  2027. if (copy_from_user(&val, optval, sizeof(val)))
  2028. return -EFAULT;
  2029. po->tp_tstamp = val;
  2030. return 0;
  2031. }
  2032. case PACKET_FANOUT:
  2033. {
  2034. int val;
  2035. if (optlen != sizeof(val))
  2036. return -EINVAL;
  2037. if (copy_from_user(&val, optval, sizeof(val)))
  2038. return -EFAULT;
  2039. return fanout_add(sk, val & 0xffff, val >> 16);
  2040. }
  2041. default:
  2042. return -ENOPROTOOPT;
  2043. }
  2044. }
  2045. static int packet_getsockopt(struct socket *sock, int level, int optname,
  2046. char __user *optval, int __user *optlen)
  2047. {
  2048. int len;
  2049. int val;
  2050. struct sock *sk = sock->sk;
  2051. struct packet_sock *po = pkt_sk(sk);
  2052. void *data;
  2053. struct tpacket_stats st;
  2054. if (level != SOL_PACKET)
  2055. return -ENOPROTOOPT;
  2056. if (get_user(len, optlen))
  2057. return -EFAULT;
  2058. if (len < 0)
  2059. return -EINVAL;
  2060. switch (optname) {
  2061. case PACKET_STATISTICS:
  2062. if (len > sizeof(struct tpacket_stats))
  2063. len = sizeof(struct tpacket_stats);
  2064. spin_lock_bh(&sk->sk_receive_queue.lock);
  2065. st = po->stats;
  2066. memset(&po->stats, 0, sizeof(st));
  2067. spin_unlock_bh(&sk->sk_receive_queue.lock);
  2068. st.tp_packets += st.tp_drops;
  2069. data = &st;
  2070. break;
  2071. case PACKET_AUXDATA:
  2072. if (len > sizeof(int))
  2073. len = sizeof(int);
  2074. val = po->auxdata;
  2075. data = &val;
  2076. break;
  2077. case PACKET_ORIGDEV:
  2078. if (len > sizeof(int))
  2079. len = sizeof(int);
  2080. val = po->origdev;
  2081. data = &val;
  2082. break;
  2083. case PACKET_VNET_HDR:
  2084. if (len > sizeof(int))
  2085. len = sizeof(int);
  2086. val = po->has_vnet_hdr;
  2087. data = &val;
  2088. break;
  2089. case PACKET_VERSION:
  2090. if (len > sizeof(int))
  2091. len = sizeof(int);
  2092. val = po->tp_version;
  2093. data = &val;
  2094. break;
  2095. case PACKET_HDRLEN:
  2096. if (len > sizeof(int))
  2097. len = sizeof(int);
  2098. if (copy_from_user(&val, optval, len))
  2099. return -EFAULT;
  2100. switch (val) {
  2101. case TPACKET_V1:
  2102. val = sizeof(struct tpacket_hdr);
  2103. break;
  2104. case TPACKET_V2:
  2105. val = sizeof(struct tpacket2_hdr);
  2106. break;
  2107. default:
  2108. return -EINVAL;
  2109. }
  2110. data = &val;
  2111. break;
  2112. case PACKET_RESERVE:
  2113. if (len > sizeof(unsigned int))
  2114. len = sizeof(unsigned int);
  2115. val = po->tp_reserve;
  2116. data = &val;
  2117. break;
  2118. case PACKET_LOSS:
  2119. if (len > sizeof(unsigned int))
  2120. len = sizeof(unsigned int);
  2121. val = po->tp_loss;
  2122. data = &val;
  2123. break;
  2124. case PACKET_TIMESTAMP:
  2125. if (len > sizeof(int))
  2126. len = sizeof(int);
  2127. val = po->tp_tstamp;
  2128. data = &val;
  2129. break;
  2130. case PACKET_FANOUT:
  2131. if (len > sizeof(int))
  2132. len = sizeof(int);
  2133. val = (po->fanout ?
  2134. ((u32)po->fanout->id |
  2135. ((u32)po->fanout->type << 16)) :
  2136. 0);
  2137. data = &val;
  2138. break;
  2139. default:
  2140. return -ENOPROTOOPT;
  2141. }
  2142. if (put_user(len, optlen))
  2143. return -EFAULT;
  2144. if (copy_to_user(optval, data, len))
  2145. return -EFAULT;
  2146. return 0;
  2147. }
  2148. static int packet_notifier(struct notifier_block *this, unsigned long msg, void *data)
  2149. {
  2150. struct sock *sk;
  2151. struct hlist_node *node;
  2152. struct net_device *dev = data;
  2153. struct net *net = dev_net(dev);
  2154. rcu_read_lock();
  2155. sk_for_each_rcu(sk, node, &net->packet.sklist) {
  2156. struct packet_sock *po = pkt_sk(sk);
  2157. switch (msg) {
  2158. case NETDEV_UNREGISTER:
  2159. if (po->mclist)
  2160. packet_dev_mclist(dev, po->mclist, -1);
  2161. /* fallthrough */
  2162. case NETDEV_DOWN:
  2163. if (dev->ifindex == po->ifindex) {
  2164. spin_lock(&po->bind_lock);
  2165. if (po->running) {
  2166. __unregister_prot_hook(sk, false);
  2167. sk->sk_err = ENETDOWN;
  2168. if (!sock_flag(sk, SOCK_DEAD))
  2169. sk->sk_error_report(sk);
  2170. }
  2171. if (msg == NETDEV_UNREGISTER) {
  2172. po->ifindex = -1;
  2173. if (po->prot_hook.dev)
  2174. dev_put(po->prot_hook.dev);
  2175. po->prot_hook.dev = NULL;
  2176. }
  2177. spin_unlock(&po->bind_lock);
  2178. }
  2179. break;
  2180. case NETDEV_UP:
  2181. if (dev->ifindex == po->ifindex) {
  2182. spin_lock(&po->bind_lock);
  2183. if (po->num)
  2184. register_prot_hook(sk);
  2185. spin_unlock(&po->bind_lock);
  2186. }
  2187. break;
  2188. }
  2189. }
  2190. rcu_read_unlock();
  2191. return NOTIFY_DONE;
  2192. }
  2193. static int packet_ioctl(struct socket *sock, unsigned int cmd,
  2194. unsigned long arg)
  2195. {
  2196. struct sock *sk = sock->sk;
  2197. switch (cmd) {
  2198. case SIOCOUTQ:
  2199. {
  2200. int amount = sk_wmem_alloc_get(sk);
  2201. return put_user(amount, (int __user *)arg);
  2202. }
  2203. case SIOCINQ:
  2204. {
  2205. struct sk_buff *skb;
  2206. int amount = 0;
  2207. spin_lock_bh(&sk->sk_receive_queue.lock);
  2208. skb = skb_peek(&sk->sk_receive_queue);
  2209. if (skb)
  2210. amount = skb->len;
  2211. spin_unlock_bh(&sk->sk_receive_queue.lock);
  2212. return put_user(amount, (int __user *)arg);
  2213. }
  2214. case SIOCGSTAMP:
  2215. return sock_get_timestamp(sk, (struct timeval __user *)arg);
  2216. case SIOCGSTAMPNS:
  2217. return sock_get_timestampns(sk, (struct timespec __user *)arg);
  2218. #ifdef CONFIG_INET
  2219. case SIOCADDRT:
  2220. case SIOCDELRT:
  2221. case SIOCDARP:
  2222. case SIOCGARP:
  2223. case SIOCSARP:
  2224. case SIOCGIFADDR:
  2225. case SIOCSIFADDR:
  2226. case SIOCGIFBRDADDR:
  2227. case SIOCSIFBRDADDR:
  2228. case SIOCGIFNETMASK:
  2229. case SIOCSIFNETMASK:
  2230. case SIOCGIFDSTADDR:
  2231. case SIOCSIFDSTADDR:
  2232. case SIOCSIFFLAGS:
  2233. return inet_dgram_ops.ioctl(sock, cmd, arg);
  2234. #endif
  2235. default:
  2236. return -ENOIOCTLCMD;
  2237. }
  2238. return 0;
  2239. }
  2240. static unsigned int packet_poll(struct file *file, struct socket *sock,
  2241. poll_table *wait)
  2242. {
  2243. struct sock *sk = sock->sk;
  2244. struct packet_sock *po = pkt_sk(sk);
  2245. unsigned int mask = datagram_poll(file, sock, wait);
  2246. spin_lock_bh(&sk->sk_receive_queue.lock);
  2247. if (po->rx_ring.pg_vec) {
  2248. if (!packet_previous_frame(po, &po->rx_ring, TP_STATUS_KERNEL))
  2249. mask |= POLLIN | POLLRDNORM;
  2250. }
  2251. spin_unlock_bh(&sk->sk_receive_queue.lock);
  2252. spin_lock_bh(&sk->sk_write_queue.lock);
  2253. if (po->tx_ring.pg_vec) {
  2254. if (packet_current_frame(po, &po->tx_ring, TP_STATUS_AVAILABLE))
  2255. mask |= POLLOUT | POLLWRNORM;
  2256. }
  2257. spin_unlock_bh(&sk->sk_write_queue.lock);
  2258. return mask;
  2259. }
  2260. /* Dirty? Well, I still did not learn better way to account
  2261. * for user mmaps.
  2262. */
  2263. static void packet_mm_open(struct vm_area_struct *vma)
  2264. {
  2265. struct file *file = vma->vm_file;
  2266. struct socket *sock = file->private_data;
  2267. struct sock *sk = sock->sk;
  2268. if (sk)
  2269. atomic_inc(&pkt_sk(sk)->mapped);
  2270. }
  2271. static void packet_mm_close(struct vm_area_struct *vma)
  2272. {
  2273. struct file *file = vma->vm_file;
  2274. struct socket *sock = file->private_data;
  2275. struct sock *sk = sock->sk;
  2276. if (sk)
  2277. atomic_dec(&pkt_sk(sk)->mapped);
  2278. }
  2279. static const struct vm_operations_struct packet_mmap_ops = {
  2280. .open = packet_mm_open,
  2281. .close = packet_mm_close,
  2282. };
  2283. static void free_pg_vec(struct pgv *pg_vec, unsigned int order,
  2284. unsigned int len)
  2285. {
  2286. int i;
  2287. for (i = 0; i < len; i++) {
  2288. if (likely(pg_vec[i].buffer)) {
  2289. if (is_vmalloc_addr(pg_vec[i].buffer))
  2290. vfree(pg_vec[i].buffer);
  2291. else
  2292. free_pages((unsigned long)pg_vec[i].buffer,
  2293. order);
  2294. pg_vec[i].buffer = NULL;
  2295. }
  2296. }
  2297. kfree(pg_vec);
  2298. }
  2299. static inline char *alloc_one_pg_vec_page(unsigned long order)
  2300. {
  2301. char *buffer = NULL;
  2302. gfp_t gfp_flags = GFP_KERNEL | __GFP_COMP |
  2303. __GFP_ZERO | __GFP_NOWARN | __GFP_NORETRY;
  2304. buffer = (char *) __get_free_pages(gfp_flags, order);
  2305. if (buffer)
  2306. return buffer;
  2307. /*
  2308. * __get_free_pages failed, fall back to vmalloc
  2309. */
  2310. buffer = vzalloc((1 << order) * PAGE_SIZE);
  2311. if (buffer)
  2312. return buffer;
  2313. /*
  2314. * vmalloc failed, lets dig into swap here
  2315. */
  2316. gfp_flags &= ~__GFP_NORETRY;
  2317. buffer = (char *)__get_free_pages(gfp_flags, order);
  2318. if (buffer)
  2319. return buffer;
  2320. /*
  2321. * complete and utter failure
  2322. */
  2323. return NULL;
  2324. }
  2325. static struct pgv *alloc_pg_vec(struct tpacket_req *req, int order)
  2326. {
  2327. unsigned int block_nr = req->tp_block_nr;
  2328. struct pgv *pg_vec;
  2329. int i;
  2330. pg_vec = kcalloc(block_nr, sizeof(struct pgv), GFP_KERNEL);
  2331. if (unlikely(!pg_vec))
  2332. goto out;
  2333. for (i = 0; i < block_nr; i++) {
  2334. pg_vec[i].buffer = alloc_one_pg_vec_page(order);
  2335. if (unlikely(!pg_vec[i].buffer))
  2336. goto out_free_pgvec;
  2337. }
  2338. out:
  2339. return pg_vec;
  2340. out_free_pgvec:
  2341. free_pg_vec(pg_vec, order, block_nr);
  2342. pg_vec = NULL;
  2343. goto out;
  2344. }
  2345. static int packet_set_ring(struct sock *sk, struct tpacket_req *req,
  2346. int closing, int tx_ring)
  2347. {
  2348. struct pgv *pg_vec = NULL;
  2349. struct packet_sock *po = pkt_sk(sk);
  2350. int was_running, order = 0;
  2351. struct packet_ring_buffer *rb;
  2352. struct sk_buff_head *rb_queue;
  2353. __be16 num;
  2354. int err;
  2355. rb = tx_ring ? &po->tx_ring : &po->rx_ring;
  2356. rb_queue = tx_ring ? &sk->sk_write_queue : &sk->sk_receive_queue;
  2357. err = -EBUSY;
  2358. if (!closing) {
  2359. if (atomic_read(&po->mapped))
  2360. goto out;
  2361. if (atomic_read(&rb->pending))
  2362. goto out;
  2363. }
  2364. if (req->tp_block_nr) {
  2365. /* Sanity tests and some calculations */
  2366. err = -EBUSY;
  2367. if (unlikely(rb->pg_vec))
  2368. goto out;
  2369. switch (po->tp_version) {
  2370. case TPACKET_V1:
  2371. po->tp_hdrlen = TPACKET_HDRLEN;
  2372. break;
  2373. case TPACKET_V2:
  2374. po->tp_hdrlen = TPACKET2_HDRLEN;
  2375. break;
  2376. }
  2377. err = -EINVAL;
  2378. if (unlikely((int)req->tp_block_size <= 0))
  2379. goto out;
  2380. if (unlikely(req->tp_block_size & (PAGE_SIZE - 1)))
  2381. goto out;
  2382. if (unlikely(req->tp_frame_size < po->tp_hdrlen +
  2383. po->tp_reserve))
  2384. goto out;
  2385. if (unlikely(req->tp_frame_size & (TPACKET_ALIGNMENT - 1)))
  2386. goto out;
  2387. rb->frames_per_block = req->tp_block_size/req->tp_frame_size;
  2388. if (unlikely(rb->frames_per_block <= 0))
  2389. goto out;
  2390. if (unlikely((rb->frames_per_block * req->tp_block_nr) !=
  2391. req->tp_frame_nr))
  2392. goto out;
  2393. err = -ENOMEM;
  2394. order = get_order(req->tp_block_size);
  2395. pg_vec = alloc_pg_vec(req, order);
  2396. if (unlikely(!pg_vec))
  2397. goto out;
  2398. }
  2399. /* Done */
  2400. else {
  2401. err = -EINVAL;
  2402. if (unlikely(req->tp_frame_nr))
  2403. goto out;
  2404. }
  2405. lock_sock(sk);
  2406. /* Detach socket from network */
  2407. spin_lock(&po->bind_lock);
  2408. was_running = po->running;
  2409. num = po->num;
  2410. if (was_running) {
  2411. po->num = 0;
  2412. __unregister_prot_hook(sk, false);
  2413. }
  2414. spin_unlock(&po->bind_lock);
  2415. synchronize_net();
  2416. err = -EBUSY;
  2417. mutex_lock(&po->pg_vec_lock);
  2418. if (closing || atomic_read(&po->mapped) == 0) {
  2419. err = 0;
  2420. spin_lock_bh(&rb_queue->lock);
  2421. swap(rb->pg_vec, pg_vec);
  2422. rb->frame_max = (req->tp_frame_nr - 1);
  2423. rb->head = 0;
  2424. rb->frame_size = req->tp_frame_size;
  2425. spin_unlock_bh(&rb_queue->lock);
  2426. swap(rb->pg_vec_order, order);
  2427. swap(rb->pg_vec_len, req->tp_block_nr);
  2428. rb->pg_vec_pages = req->tp_block_size/PAGE_SIZE;
  2429. po->prot_hook.func = (po->rx_ring.pg_vec) ?
  2430. tpacket_rcv : packet_rcv;
  2431. skb_queue_purge(rb_queue);
  2432. if (atomic_read(&po->mapped))
  2433. pr_err("packet_mmap: vma is busy: %d\n",
  2434. atomic_read(&po->mapped));
  2435. }
  2436. mutex_unlock(&po->pg_vec_lock);
  2437. spin_lock(&po->bind_lock);
  2438. if (was_running) {
  2439. po->num = num;
  2440. register_prot_hook(sk);
  2441. }
  2442. spin_unlock(&po->bind_lock);
  2443. release_sock(sk);
  2444. if (pg_vec)
  2445. free_pg_vec(pg_vec, order, req->tp_block_nr);
  2446. out:
  2447. return err;
  2448. }
  2449. static int packet_mmap(struct file *file, struct socket *sock,
  2450. struct vm_area_struct *vma)
  2451. {
  2452. struct sock *sk = sock->sk;
  2453. struct packet_sock *po = pkt_sk(sk);
  2454. unsigned long size, expected_size;
  2455. struct packet_ring_buffer *rb;
  2456. unsigned long start;
  2457. int err = -EINVAL;
  2458. int i;
  2459. if (vma->vm_pgoff)
  2460. return -EINVAL;
  2461. mutex_lock(&po->pg_vec_lock);
  2462. expected_size = 0;
  2463. for (rb = &po->rx_ring; rb <= &po->tx_ring; rb++) {
  2464. if (rb->pg_vec) {
  2465. expected_size += rb->pg_vec_len
  2466. * rb->pg_vec_pages
  2467. * PAGE_SIZE;
  2468. }
  2469. }
  2470. if (expected_size == 0)
  2471. goto out;
  2472. size = vma->vm_end - vma->vm_start;
  2473. if (size != expected_size)
  2474. goto out;
  2475. start = vma->vm_start;
  2476. for (rb = &po->rx_ring; rb <= &po->tx_ring; rb++) {
  2477. if (rb->pg_vec == NULL)
  2478. continue;
  2479. for (i = 0; i < rb->pg_vec_len; i++) {
  2480. struct page *page;
  2481. void *kaddr = rb->pg_vec[i].buffer;
  2482. int pg_num;
  2483. for (pg_num = 0; pg_num < rb->pg_vec_pages; pg_num++) {
  2484. page = pgv_to_page(kaddr);
  2485. err = vm_insert_page(vma, start, page);
  2486. if (unlikely(err))
  2487. goto out;
  2488. start += PAGE_SIZE;
  2489. kaddr += PAGE_SIZE;
  2490. }
  2491. }
  2492. }
  2493. atomic_inc(&po->mapped);
  2494. vma->vm_ops = &packet_mmap_ops;
  2495. err = 0;
  2496. out:
  2497. mutex_unlock(&po->pg_vec_lock);
  2498. return err;
  2499. }
  2500. static const struct proto_ops packet_ops_spkt = {
  2501. .family = PF_PACKET,
  2502. .owner = THIS_MODULE,
  2503. .release = packet_release,
  2504. .bind = packet_bind_spkt,
  2505. .connect = sock_no_connect,
  2506. .socketpair = sock_no_socketpair,
  2507. .accept = sock_no_accept,
  2508. .getname = packet_getname_spkt,
  2509. .poll = datagram_poll,
  2510. .ioctl = packet_ioctl,
  2511. .listen = sock_no_listen,
  2512. .shutdown = sock_no_shutdown,
  2513. .setsockopt = sock_no_setsockopt,
  2514. .getsockopt = sock_no_getsockopt,
  2515. .sendmsg = packet_sendmsg_spkt,
  2516. .recvmsg = packet_recvmsg,
  2517. .mmap = sock_no_mmap,
  2518. .sendpage = sock_no_sendpage,
  2519. };
  2520. static const struct proto_ops packet_ops = {
  2521. .family = PF_PACKET,
  2522. .owner = THIS_MODULE,
  2523. .release = packet_release,
  2524. .bind = packet_bind,
  2525. .connect = sock_no_connect,
  2526. .socketpair = sock_no_socketpair,
  2527. .accept = sock_no_accept,
  2528. .getname = packet_getname,
  2529. .poll = packet_poll,
  2530. .ioctl = packet_ioctl,
  2531. .listen = sock_no_listen,
  2532. .shutdown = sock_no_shutdown,
  2533. .setsockopt = packet_setsockopt,
  2534. .getsockopt = packet_getsockopt,
  2535. .sendmsg = packet_sendmsg,
  2536. .recvmsg = packet_recvmsg,
  2537. .mmap = packet_mmap,
  2538. .sendpage = sock_no_sendpage,
  2539. };
  2540. static const struct net_proto_family packet_family_ops = {
  2541. .family = PF_PACKET,
  2542. .create = packet_create,
  2543. .owner = THIS_MODULE,
  2544. };
  2545. static struct notifier_block packet_netdev_notifier = {
  2546. .notifier_call = packet_notifier,
  2547. };
  2548. #ifdef CONFIG_PROC_FS
  2549. static void *packet_seq_start(struct seq_file *seq, loff_t *pos)
  2550. __acquires(RCU)
  2551. {
  2552. struct net *net = seq_file_net(seq);
  2553. rcu_read_lock();
  2554. return seq_hlist_start_head_rcu(&net->packet.sklist, *pos);
  2555. }
  2556. static void *packet_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  2557. {
  2558. struct net *net = seq_file_net(seq);
  2559. return seq_hlist_next_rcu(v, &net->packet.sklist, pos);
  2560. }
  2561. static void packet_seq_stop(struct seq_file *seq, void *v)
  2562. __releases(RCU)
  2563. {
  2564. rcu_read_unlock();
  2565. }
  2566. static int packet_seq_show(struct seq_file *seq, void *v)
  2567. {
  2568. if (v == SEQ_START_TOKEN)
  2569. seq_puts(seq, "sk RefCnt Type Proto Iface R Rmem User Inode\n");
  2570. else {
  2571. struct sock *s = sk_entry(v);
  2572. const struct packet_sock *po = pkt_sk(s);
  2573. seq_printf(seq,
  2574. "%pK %-6d %-4d %04x %-5d %1d %-6u %-6u %-6lu\n",
  2575. s,
  2576. atomic_read(&s->sk_refcnt),
  2577. s->sk_type,
  2578. ntohs(po->num),
  2579. po->ifindex,
  2580. po->running,
  2581. atomic_read(&s->sk_rmem_alloc),
  2582. sock_i_uid(s),
  2583. sock_i_ino(s));
  2584. }
  2585. return 0;
  2586. }
  2587. static const struct seq_operations packet_seq_ops = {
  2588. .start = packet_seq_start,
  2589. .next = packet_seq_next,
  2590. .stop = packet_seq_stop,
  2591. .show = packet_seq_show,
  2592. };
  2593. static int packet_seq_open(struct inode *inode, struct file *file)
  2594. {
  2595. return seq_open_net(inode, file, &packet_seq_ops,
  2596. sizeof(struct seq_net_private));
  2597. }
  2598. static const struct file_operations packet_seq_fops = {
  2599. .owner = THIS_MODULE,
  2600. .open = packet_seq_open,
  2601. .read = seq_read,
  2602. .llseek = seq_lseek,
  2603. .release = seq_release_net,
  2604. };
  2605. #endif
  2606. static int __net_init packet_net_init(struct net *net)
  2607. {
  2608. spin_lock_init(&net->packet.sklist_lock);
  2609. INIT_HLIST_HEAD(&net->packet.sklist);
  2610. if (!proc_net_fops_create(net, "packet", 0, &packet_seq_fops))
  2611. return -ENOMEM;
  2612. return 0;
  2613. }
  2614. static void __net_exit packet_net_exit(struct net *net)
  2615. {
  2616. proc_net_remove(net, "packet");
  2617. }
  2618. static struct pernet_operations packet_net_ops = {
  2619. .init = packet_net_init,
  2620. .exit = packet_net_exit,
  2621. };
  2622. static void __exit packet_exit(void)
  2623. {
  2624. unregister_netdevice_notifier(&packet_netdev_notifier);
  2625. unregister_pernet_subsys(&packet_net_ops);
  2626. sock_unregister(PF_PACKET);
  2627. proto_unregister(&packet_proto);
  2628. }
  2629. static int __init packet_init(void)
  2630. {
  2631. int rc = proto_register(&packet_proto, 0);
  2632. if (rc != 0)
  2633. goto out;
  2634. sock_register(&packet_family_ops);
  2635. register_pernet_subsys(&packet_net_ops);
  2636. register_netdevice_notifier(&packet_netdev_notifier);
  2637. out:
  2638. return rc;
  2639. }
  2640. module_init(packet_init);
  2641. module_exit(packet_exit);
  2642. MODULE_LICENSE("GPL");
  2643. MODULE_ALIAS_NETPROTO(PF_PACKET);