udp.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909
  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. * The User Datagram Protocol (UDP).
  7. *
  8. * Authors: Ross Biro
  9. * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  10. * Arnt Gulbrandsen, <agulbra@nvg.unit.no>
  11. * Alan Cox, <alan@lxorguk.ukuu.org.uk>
  12. * Hirokazu Takahashi, <taka@valinux.co.jp>
  13. *
  14. * Fixes:
  15. * Alan Cox : verify_area() calls
  16. * Alan Cox : stopped close while in use off icmp
  17. * messages. Not a fix but a botch that
  18. * for udp at least is 'valid'.
  19. * Alan Cox : Fixed icmp handling properly
  20. * Alan Cox : Correct error for oversized datagrams
  21. * Alan Cox : Tidied select() semantics.
  22. * Alan Cox : udp_err() fixed properly, also now
  23. * select and read wake correctly on errors
  24. * Alan Cox : udp_send verify_area moved to avoid mem leak
  25. * Alan Cox : UDP can count its memory
  26. * Alan Cox : send to an unknown connection causes
  27. * an ECONNREFUSED off the icmp, but
  28. * does NOT close.
  29. * Alan Cox : Switched to new sk_buff handlers. No more backlog!
  30. * Alan Cox : Using generic datagram code. Even smaller and the PEEK
  31. * bug no longer crashes it.
  32. * Fred Van Kempen : Net2e support for sk->broadcast.
  33. * Alan Cox : Uses skb_free_datagram
  34. * Alan Cox : Added get/set sockopt support.
  35. * Alan Cox : Broadcasting without option set returns EACCES.
  36. * Alan Cox : No wakeup calls. Instead we now use the callbacks.
  37. * Alan Cox : Use ip_tos and ip_ttl
  38. * Alan Cox : SNMP Mibs
  39. * Alan Cox : MSG_DONTROUTE, and 0.0.0.0 support.
  40. * Matt Dillon : UDP length checks.
  41. * Alan Cox : Smarter af_inet used properly.
  42. * Alan Cox : Use new kernel side addressing.
  43. * Alan Cox : Incorrect return on truncated datagram receive.
  44. * Arnt Gulbrandsen : New udp_send and stuff
  45. * Alan Cox : Cache last socket
  46. * Alan Cox : Route cache
  47. * Jon Peatfield : Minor efficiency fix to sendto().
  48. * Mike Shaver : RFC1122 checks.
  49. * Alan Cox : Nonblocking error fix.
  50. * Willy Konynenberg : Transparent proxying support.
  51. * Mike McLagan : Routing by source
  52. * David S. Miller : New socket lookup architecture.
  53. * Last socket cache retained as it
  54. * does have a high hit rate.
  55. * Olaf Kirch : Don't linearise iovec on sendmsg.
  56. * Andi Kleen : Some cleanups, cache destination entry
  57. * for connect.
  58. * Vitaly E. Lavrov : Transparent proxy revived after year coma.
  59. * Melvin Smith : Check msg_name not msg_namelen in sendto(),
  60. * return ENOTCONN for unconnected sockets (POSIX)
  61. * Janos Farkas : don't deliver multi/broadcasts to a different
  62. * bound-to-device socket
  63. * Hirokazu Takahashi : HW checksumming for outgoing UDP
  64. * datagrams.
  65. * Hirokazu Takahashi : sendfile() on UDP works now.
  66. * Arnaldo C. Melo : convert /proc/net/udp to seq_file
  67. * YOSHIFUJI Hideaki @USAGI and: Support IPV6_V6ONLY socket option, which
  68. * Alexey Kuznetsov: allow both IPv4 and IPv6 sockets to bind
  69. * a single port at the same time.
  70. * Derek Atkins <derek@ihtfp.com>: Add Encapulation Support
  71. * James Chapman : Add L2TP encapsulation type.
  72. *
  73. *
  74. * This program is free software; you can redistribute it and/or
  75. * modify it under the terms of the GNU General Public License
  76. * as published by the Free Software Foundation; either version
  77. * 2 of the License, or (at your option) any later version.
  78. */
  79. #include <asm/system.h>
  80. #include <asm/uaccess.h>
  81. #include <asm/ioctls.h>
  82. #include <linux/bootmem.h>
  83. #include <linux/highmem.h>
  84. #include <linux/swap.h>
  85. #include <linux/types.h>
  86. #include <linux/fcntl.h>
  87. #include <linux/module.h>
  88. #include <linux/socket.h>
  89. #include <linux/sockios.h>
  90. #include <linux/igmp.h>
  91. #include <linux/in.h>
  92. #include <linux/errno.h>
  93. #include <linux/timer.h>
  94. #include <linux/mm.h>
  95. #include <linux/inet.h>
  96. #include <linux/netdevice.h>
  97. #include <net/tcp_states.h>
  98. #include <linux/skbuff.h>
  99. #include <linux/proc_fs.h>
  100. #include <linux/seq_file.h>
  101. #include <net/net_namespace.h>
  102. #include <net/icmp.h>
  103. #include <net/route.h>
  104. #include <net/checksum.h>
  105. #include <net/xfrm.h>
  106. #include "udp_impl.h"
  107. struct udp_table udp_table;
  108. EXPORT_SYMBOL(udp_table);
  109. int sysctl_udp_mem[3] __read_mostly;
  110. EXPORT_SYMBOL(sysctl_udp_mem);
  111. int sysctl_udp_rmem_min __read_mostly;
  112. EXPORT_SYMBOL(sysctl_udp_rmem_min);
  113. int sysctl_udp_wmem_min __read_mostly;
  114. EXPORT_SYMBOL(sysctl_udp_wmem_min);
  115. atomic_t udp_memory_allocated;
  116. EXPORT_SYMBOL(udp_memory_allocated);
  117. #define PORTS_PER_CHAIN (65536 / UDP_HTABLE_SIZE)
  118. static int udp_lib_lport_inuse(struct net *net, __u16 num,
  119. const struct udp_hslot *hslot,
  120. unsigned long *bitmap,
  121. struct sock *sk,
  122. int (*saddr_comp)(const struct sock *sk1,
  123. const struct sock *sk2))
  124. {
  125. struct sock *sk2;
  126. struct hlist_nulls_node *node;
  127. sk_nulls_for_each(sk2, node, &hslot->head)
  128. if (net_eq(sock_net(sk2), net) &&
  129. sk2 != sk &&
  130. (bitmap || sk2->sk_hash == num) &&
  131. (!sk2->sk_reuse || !sk->sk_reuse) &&
  132. (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if
  133. || sk2->sk_bound_dev_if == sk->sk_bound_dev_if) &&
  134. (*saddr_comp)(sk, sk2)) {
  135. if (bitmap)
  136. __set_bit(sk2->sk_hash / UDP_HTABLE_SIZE,
  137. bitmap);
  138. else
  139. return 1;
  140. }
  141. return 0;
  142. }
  143. /**
  144. * udp_lib_get_port - UDP/-Lite port lookup for IPv4 and IPv6
  145. *
  146. * @sk: socket struct in question
  147. * @snum: port number to look up
  148. * @saddr_comp: AF-dependent comparison of bound local IP addresses
  149. */
  150. int udp_lib_get_port(struct sock *sk, unsigned short snum,
  151. int (*saddr_comp)(const struct sock *sk1,
  152. const struct sock *sk2))
  153. {
  154. struct udp_hslot *hslot;
  155. struct udp_table *udptable = sk->sk_prot->h.udp_table;
  156. int error = 1;
  157. struct net *net = sock_net(sk);
  158. if (!snum) {
  159. int low, high, remaining;
  160. unsigned rand;
  161. unsigned short first, last;
  162. DECLARE_BITMAP(bitmap, PORTS_PER_CHAIN);
  163. inet_get_local_port_range(&low, &high);
  164. remaining = (high - low) + 1;
  165. rand = net_random();
  166. first = (((u64)rand * remaining) >> 32) + low;
  167. /*
  168. * force rand to be an odd multiple of UDP_HTABLE_SIZE
  169. */
  170. rand = (rand | 1) * UDP_HTABLE_SIZE;
  171. for (last = first + UDP_HTABLE_SIZE; first != last; first++) {
  172. hslot = &udptable->hash[udp_hashfn(net, first)];
  173. bitmap_zero(bitmap, PORTS_PER_CHAIN);
  174. spin_lock_bh(&hslot->lock);
  175. udp_lib_lport_inuse(net, snum, hslot, bitmap, sk,
  176. saddr_comp);
  177. snum = first;
  178. /*
  179. * Iterate on all possible values of snum for this hash.
  180. * Using steps of an odd multiple of UDP_HTABLE_SIZE
  181. * give us randomization and full range coverage.
  182. */
  183. do {
  184. if (low <= snum && snum <= high &&
  185. !test_bit(snum / UDP_HTABLE_SIZE, bitmap))
  186. goto found;
  187. snum += rand;
  188. } while (snum != first);
  189. spin_unlock_bh(&hslot->lock);
  190. }
  191. goto fail;
  192. } else {
  193. hslot = &udptable->hash[udp_hashfn(net, snum)];
  194. spin_lock_bh(&hslot->lock);
  195. if (udp_lib_lport_inuse(net, snum, hslot, NULL, sk, saddr_comp))
  196. goto fail_unlock;
  197. }
  198. found:
  199. inet_sk(sk)->num = snum;
  200. sk->sk_hash = snum;
  201. if (sk_unhashed(sk)) {
  202. sk_nulls_add_node_rcu(sk, &hslot->head);
  203. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
  204. }
  205. error = 0;
  206. fail_unlock:
  207. spin_unlock_bh(&hslot->lock);
  208. fail:
  209. return error;
  210. }
  211. EXPORT_SYMBOL(udp_lib_get_port);
  212. static int ipv4_rcv_saddr_equal(const struct sock *sk1, const struct sock *sk2)
  213. {
  214. struct inet_sock *inet1 = inet_sk(sk1), *inet2 = inet_sk(sk2);
  215. return (!ipv6_only_sock(sk2) &&
  216. (!inet1->rcv_saddr || !inet2->rcv_saddr ||
  217. inet1->rcv_saddr == inet2->rcv_saddr));
  218. }
  219. int udp_v4_get_port(struct sock *sk, unsigned short snum)
  220. {
  221. return udp_lib_get_port(sk, snum, ipv4_rcv_saddr_equal);
  222. }
  223. static inline int compute_score(struct sock *sk, struct net *net, __be32 saddr,
  224. unsigned short hnum,
  225. __be16 sport, __be32 daddr, __be16 dport, int dif)
  226. {
  227. int score = -1;
  228. if (net_eq(sock_net(sk), net) && sk->sk_hash == hnum &&
  229. !ipv6_only_sock(sk)) {
  230. struct inet_sock *inet = inet_sk(sk);
  231. score = (sk->sk_family == PF_INET ? 1 : 0);
  232. if (inet->rcv_saddr) {
  233. if (inet->rcv_saddr != daddr)
  234. return -1;
  235. score += 2;
  236. }
  237. if (inet->daddr) {
  238. if (inet->daddr != saddr)
  239. return -1;
  240. score += 2;
  241. }
  242. if (inet->dport) {
  243. if (inet->dport != sport)
  244. return -1;
  245. score += 2;
  246. }
  247. if (sk->sk_bound_dev_if) {
  248. if (sk->sk_bound_dev_if != dif)
  249. return -1;
  250. score += 2;
  251. }
  252. }
  253. return score;
  254. }
  255. /* UDP is nearly always wildcards out the wazoo, it makes no sense to try
  256. * harder than this. -DaveM
  257. */
  258. static struct sock *__udp4_lib_lookup(struct net *net, __be32 saddr,
  259. __be16 sport, __be32 daddr, __be16 dport,
  260. int dif, struct udp_table *udptable)
  261. {
  262. struct sock *sk, *result;
  263. struct hlist_nulls_node *node;
  264. unsigned short hnum = ntohs(dport);
  265. unsigned int hash = udp_hashfn(net, hnum);
  266. struct udp_hslot *hslot = &udptable->hash[hash];
  267. int score, badness;
  268. rcu_read_lock();
  269. begin:
  270. result = NULL;
  271. badness = -1;
  272. sk_nulls_for_each_rcu(sk, node, &hslot->head) {
  273. score = compute_score(sk, net, saddr, hnum, sport,
  274. daddr, dport, dif);
  275. if (score > badness) {
  276. result = sk;
  277. badness = score;
  278. }
  279. }
  280. /*
  281. * if the nulls value we got at the end of this lookup is
  282. * not the expected one, we must restart lookup.
  283. * We probably met an item that was moved to another chain.
  284. */
  285. if (get_nulls_value(node) != hash)
  286. goto begin;
  287. if (result) {
  288. if (unlikely(!atomic_inc_not_zero(&result->sk_refcnt)))
  289. result = NULL;
  290. else if (unlikely(compute_score(result, net, saddr, hnum, sport,
  291. daddr, dport, dif) < badness)) {
  292. sock_put(result);
  293. goto begin;
  294. }
  295. }
  296. rcu_read_unlock();
  297. return result;
  298. }
  299. static inline struct sock *__udp4_lib_lookup_skb(struct sk_buff *skb,
  300. __be16 sport, __be16 dport,
  301. struct udp_table *udptable)
  302. {
  303. struct sock *sk;
  304. const struct iphdr *iph = ip_hdr(skb);
  305. if (unlikely(sk = skb_steal_sock(skb)))
  306. return sk;
  307. else
  308. return __udp4_lib_lookup(dev_net(skb_dst(skb)->dev), iph->saddr, sport,
  309. iph->daddr, dport, inet_iif(skb),
  310. udptable);
  311. }
  312. struct sock *udp4_lib_lookup(struct net *net, __be32 saddr, __be16 sport,
  313. __be32 daddr, __be16 dport, int dif)
  314. {
  315. return __udp4_lib_lookup(net, saddr, sport, daddr, dport, dif, &udp_table);
  316. }
  317. EXPORT_SYMBOL_GPL(udp4_lib_lookup);
  318. static inline struct sock *udp_v4_mcast_next(struct net *net, struct sock *sk,
  319. __be16 loc_port, __be32 loc_addr,
  320. __be16 rmt_port, __be32 rmt_addr,
  321. int dif)
  322. {
  323. struct hlist_nulls_node *node;
  324. struct sock *s = sk;
  325. unsigned short hnum = ntohs(loc_port);
  326. sk_nulls_for_each_from(s, node) {
  327. struct inet_sock *inet = inet_sk(s);
  328. if (!net_eq(sock_net(s), net) ||
  329. s->sk_hash != hnum ||
  330. (inet->daddr && inet->daddr != rmt_addr) ||
  331. (inet->dport != rmt_port && inet->dport) ||
  332. (inet->rcv_saddr && inet->rcv_saddr != loc_addr) ||
  333. ipv6_only_sock(s) ||
  334. (s->sk_bound_dev_if && s->sk_bound_dev_if != dif))
  335. continue;
  336. if (!ip_mc_sf_allow(s, loc_addr, rmt_addr, dif))
  337. continue;
  338. goto found;
  339. }
  340. s = NULL;
  341. found:
  342. return s;
  343. }
  344. /*
  345. * This routine is called by the ICMP module when it gets some
  346. * sort of error condition. If err < 0 then the socket should
  347. * be closed and the error returned to the user. If err > 0
  348. * it's just the icmp type << 8 | icmp code.
  349. * Header points to the ip header of the error packet. We move
  350. * on past this. Then (as it used to claim before adjustment)
  351. * header points to the first 8 bytes of the udp header. We need
  352. * to find the appropriate port.
  353. */
  354. void __udp4_lib_err(struct sk_buff *skb, u32 info, struct udp_table *udptable)
  355. {
  356. struct inet_sock *inet;
  357. struct iphdr *iph = (struct iphdr *)skb->data;
  358. struct udphdr *uh = (struct udphdr *)(skb->data+(iph->ihl<<2));
  359. const int type = icmp_hdr(skb)->type;
  360. const int code = icmp_hdr(skb)->code;
  361. struct sock *sk;
  362. int harderr;
  363. int err;
  364. struct net *net = dev_net(skb->dev);
  365. sk = __udp4_lib_lookup(net, iph->daddr, uh->dest,
  366. iph->saddr, uh->source, skb->dev->ifindex, udptable);
  367. if (sk == NULL) {
  368. ICMP_INC_STATS_BH(net, ICMP_MIB_INERRORS);
  369. return; /* No socket for error */
  370. }
  371. err = 0;
  372. harderr = 0;
  373. inet = inet_sk(sk);
  374. switch (type) {
  375. default:
  376. case ICMP_TIME_EXCEEDED:
  377. err = EHOSTUNREACH;
  378. break;
  379. case ICMP_SOURCE_QUENCH:
  380. goto out;
  381. case ICMP_PARAMETERPROB:
  382. err = EPROTO;
  383. harderr = 1;
  384. break;
  385. case ICMP_DEST_UNREACH:
  386. if (code == ICMP_FRAG_NEEDED) { /* Path MTU discovery */
  387. if (inet->pmtudisc != IP_PMTUDISC_DONT) {
  388. err = EMSGSIZE;
  389. harderr = 1;
  390. break;
  391. }
  392. goto out;
  393. }
  394. err = EHOSTUNREACH;
  395. if (code <= NR_ICMP_UNREACH) {
  396. harderr = icmp_err_convert[code].fatal;
  397. err = icmp_err_convert[code].errno;
  398. }
  399. break;
  400. }
  401. /*
  402. * RFC1122: OK. Passes ICMP errors back to application, as per
  403. * 4.1.3.3.
  404. */
  405. if (!inet->recverr) {
  406. if (!harderr || sk->sk_state != TCP_ESTABLISHED)
  407. goto out;
  408. } else {
  409. ip_icmp_error(sk, skb, err, uh->dest, info, (u8 *)(uh+1));
  410. }
  411. sk->sk_err = err;
  412. sk->sk_error_report(sk);
  413. out:
  414. sock_put(sk);
  415. }
  416. void udp_err(struct sk_buff *skb, u32 info)
  417. {
  418. __udp4_lib_err(skb, info, &udp_table);
  419. }
  420. /*
  421. * Throw away all pending data and cancel the corking. Socket is locked.
  422. */
  423. void udp_flush_pending_frames(struct sock *sk)
  424. {
  425. struct udp_sock *up = udp_sk(sk);
  426. if (up->pending) {
  427. up->len = 0;
  428. up->pending = 0;
  429. ip_flush_pending_frames(sk);
  430. }
  431. }
  432. EXPORT_SYMBOL(udp_flush_pending_frames);
  433. /**
  434. * udp4_hwcsum_outgoing - handle outgoing HW checksumming
  435. * @sk: socket we are sending on
  436. * @skb: sk_buff containing the filled-in UDP header
  437. * (checksum field must be zeroed out)
  438. */
  439. static void udp4_hwcsum_outgoing(struct sock *sk, struct sk_buff *skb,
  440. __be32 src, __be32 dst, int len)
  441. {
  442. unsigned int offset;
  443. struct udphdr *uh = udp_hdr(skb);
  444. __wsum csum = 0;
  445. if (skb_queue_len(&sk->sk_write_queue) == 1) {
  446. /*
  447. * Only one fragment on the socket.
  448. */
  449. skb->csum_start = skb_transport_header(skb) - skb->head;
  450. skb->csum_offset = offsetof(struct udphdr, check);
  451. uh->check = ~csum_tcpudp_magic(src, dst, len, IPPROTO_UDP, 0);
  452. } else {
  453. /*
  454. * HW-checksum won't work as there are two or more
  455. * fragments on the socket so that all csums of sk_buffs
  456. * should be together
  457. */
  458. offset = skb_transport_offset(skb);
  459. skb->csum = skb_checksum(skb, offset, skb->len - offset, 0);
  460. skb->ip_summed = CHECKSUM_NONE;
  461. skb_queue_walk(&sk->sk_write_queue, skb) {
  462. csum = csum_add(csum, skb->csum);
  463. }
  464. uh->check = csum_tcpudp_magic(src, dst, len, IPPROTO_UDP, csum);
  465. if (uh->check == 0)
  466. uh->check = CSUM_MANGLED_0;
  467. }
  468. }
  469. /*
  470. * Push out all pending data as one UDP datagram. Socket is locked.
  471. */
  472. static int udp_push_pending_frames(struct sock *sk)
  473. {
  474. struct udp_sock *up = udp_sk(sk);
  475. struct inet_sock *inet = inet_sk(sk);
  476. struct flowi *fl = &inet->cork.fl;
  477. struct sk_buff *skb;
  478. struct udphdr *uh;
  479. int err = 0;
  480. int is_udplite = IS_UDPLITE(sk);
  481. __wsum csum = 0;
  482. /* Grab the skbuff where UDP header space exists. */
  483. if ((skb = skb_peek(&sk->sk_write_queue)) == NULL)
  484. goto out;
  485. /*
  486. * Create a UDP header
  487. */
  488. uh = udp_hdr(skb);
  489. uh->source = fl->fl_ip_sport;
  490. uh->dest = fl->fl_ip_dport;
  491. uh->len = htons(up->len);
  492. uh->check = 0;
  493. if (is_udplite) /* UDP-Lite */
  494. csum = udplite_csum_outgoing(sk, skb);
  495. else if (sk->sk_no_check == UDP_CSUM_NOXMIT) { /* UDP csum disabled */
  496. skb->ip_summed = CHECKSUM_NONE;
  497. goto send;
  498. } else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */
  499. udp4_hwcsum_outgoing(sk, skb, fl->fl4_src, fl->fl4_dst, up->len);
  500. goto send;
  501. } else /* `normal' UDP */
  502. csum = udp_csum_outgoing(sk, skb);
  503. /* add protocol-dependent pseudo-header */
  504. uh->check = csum_tcpudp_magic(fl->fl4_src, fl->fl4_dst, up->len,
  505. sk->sk_protocol, csum);
  506. if (uh->check == 0)
  507. uh->check = CSUM_MANGLED_0;
  508. send:
  509. err = ip_push_pending_frames(sk);
  510. if (err) {
  511. if (err == -ENOBUFS && !inet->recverr) {
  512. UDP_INC_STATS_USER(sock_net(sk),
  513. UDP_MIB_SNDBUFERRORS, is_udplite);
  514. err = 0;
  515. }
  516. } else
  517. UDP_INC_STATS_USER(sock_net(sk),
  518. UDP_MIB_OUTDATAGRAMS, is_udplite);
  519. out:
  520. up->len = 0;
  521. up->pending = 0;
  522. return err;
  523. }
  524. int udp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
  525. size_t len)
  526. {
  527. struct inet_sock *inet = inet_sk(sk);
  528. struct udp_sock *up = udp_sk(sk);
  529. int ulen = len;
  530. struct ipcm_cookie ipc;
  531. struct rtable *rt = NULL;
  532. int free = 0;
  533. int connected = 0;
  534. __be32 daddr, faddr, saddr;
  535. __be16 dport;
  536. u8 tos;
  537. int err, is_udplite = IS_UDPLITE(sk);
  538. int corkreq = up->corkflag || msg->msg_flags&MSG_MORE;
  539. int (*getfrag)(void *, char *, int, int, int, struct sk_buff *);
  540. if (len > 0xFFFF)
  541. return -EMSGSIZE;
  542. /*
  543. * Check the flags.
  544. */
  545. if (msg->msg_flags & MSG_OOB) /* Mirror BSD error message compatibility */
  546. return -EOPNOTSUPP;
  547. ipc.opt = NULL;
  548. ipc.shtx.flags = 0;
  549. if (up->pending) {
  550. /*
  551. * There are pending frames.
  552. * The socket lock must be held while it's corked.
  553. */
  554. lock_sock(sk);
  555. if (likely(up->pending)) {
  556. if (unlikely(up->pending != AF_INET)) {
  557. release_sock(sk);
  558. return -EINVAL;
  559. }
  560. goto do_append_data;
  561. }
  562. release_sock(sk);
  563. }
  564. ulen += sizeof(struct udphdr);
  565. /*
  566. * Get and verify the address.
  567. */
  568. if (msg->msg_name) {
  569. struct sockaddr_in * usin = (struct sockaddr_in *)msg->msg_name;
  570. if (msg->msg_namelen < sizeof(*usin))
  571. return -EINVAL;
  572. if (usin->sin_family != AF_INET) {
  573. if (usin->sin_family != AF_UNSPEC)
  574. return -EAFNOSUPPORT;
  575. }
  576. daddr = usin->sin_addr.s_addr;
  577. dport = usin->sin_port;
  578. if (dport == 0)
  579. return -EINVAL;
  580. } else {
  581. if (sk->sk_state != TCP_ESTABLISHED)
  582. return -EDESTADDRREQ;
  583. daddr = inet->daddr;
  584. dport = inet->dport;
  585. /* Open fast path for connected socket.
  586. Route will not be used, if at least one option is set.
  587. */
  588. connected = 1;
  589. }
  590. ipc.addr = inet->saddr;
  591. ipc.oif = sk->sk_bound_dev_if;
  592. err = sock_tx_timestamp(msg, sk, &ipc.shtx);
  593. if (err)
  594. return err;
  595. if (msg->msg_controllen) {
  596. err = ip_cmsg_send(sock_net(sk), msg, &ipc);
  597. if (err)
  598. return err;
  599. if (ipc.opt)
  600. free = 1;
  601. connected = 0;
  602. }
  603. if (!ipc.opt)
  604. ipc.opt = inet->opt;
  605. saddr = ipc.addr;
  606. ipc.addr = faddr = daddr;
  607. if (ipc.opt && ipc.opt->srr) {
  608. if (!daddr)
  609. return -EINVAL;
  610. faddr = ipc.opt->faddr;
  611. connected = 0;
  612. }
  613. tos = RT_TOS(inet->tos);
  614. if (sock_flag(sk, SOCK_LOCALROUTE) ||
  615. (msg->msg_flags & MSG_DONTROUTE) ||
  616. (ipc.opt && ipc.opt->is_strictroute)) {
  617. tos |= RTO_ONLINK;
  618. connected = 0;
  619. }
  620. if (ipv4_is_multicast(daddr)) {
  621. if (!ipc.oif)
  622. ipc.oif = inet->mc_index;
  623. if (!saddr)
  624. saddr = inet->mc_addr;
  625. connected = 0;
  626. }
  627. if (connected)
  628. rt = (struct rtable *)sk_dst_check(sk, 0);
  629. if (rt == NULL) {
  630. struct flowi fl = { .oif = ipc.oif,
  631. .mark = sk->sk_mark,
  632. .nl_u = { .ip4_u =
  633. { .daddr = faddr,
  634. .saddr = saddr,
  635. .tos = tos } },
  636. .proto = sk->sk_protocol,
  637. .flags = inet_sk_flowi_flags(sk),
  638. .uli_u = { .ports =
  639. { .sport = inet->sport,
  640. .dport = dport } } };
  641. struct net *net = sock_net(sk);
  642. security_sk_classify_flow(sk, &fl);
  643. err = ip_route_output_flow(net, &rt, &fl, sk, 1);
  644. if (err) {
  645. if (err == -ENETUNREACH)
  646. IP_INC_STATS_BH(net, IPSTATS_MIB_OUTNOROUTES);
  647. goto out;
  648. }
  649. err = -EACCES;
  650. if ((rt->rt_flags & RTCF_BROADCAST) &&
  651. !sock_flag(sk, SOCK_BROADCAST))
  652. goto out;
  653. if (connected)
  654. sk_dst_set(sk, dst_clone(&rt->u.dst));
  655. }
  656. if (msg->msg_flags&MSG_CONFIRM)
  657. goto do_confirm;
  658. back_from_confirm:
  659. saddr = rt->rt_src;
  660. if (!ipc.addr)
  661. daddr = ipc.addr = rt->rt_dst;
  662. lock_sock(sk);
  663. if (unlikely(up->pending)) {
  664. /* The socket is already corked while preparing it. */
  665. /* ... which is an evident application bug. --ANK */
  666. release_sock(sk);
  667. LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 2\n");
  668. err = -EINVAL;
  669. goto out;
  670. }
  671. /*
  672. * Now cork the socket to pend data.
  673. */
  674. inet->cork.fl.fl4_dst = daddr;
  675. inet->cork.fl.fl_ip_dport = dport;
  676. inet->cork.fl.fl4_src = saddr;
  677. inet->cork.fl.fl_ip_sport = inet->sport;
  678. up->pending = AF_INET;
  679. do_append_data:
  680. up->len += ulen;
  681. getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag;
  682. err = ip_append_data(sk, getfrag, msg->msg_iov, ulen,
  683. sizeof(struct udphdr), &ipc, &rt,
  684. corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags);
  685. if (err)
  686. udp_flush_pending_frames(sk);
  687. else if (!corkreq)
  688. err = udp_push_pending_frames(sk);
  689. else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
  690. up->pending = 0;
  691. release_sock(sk);
  692. out:
  693. ip_rt_put(rt);
  694. if (free)
  695. kfree(ipc.opt);
  696. if (!err)
  697. return len;
  698. /*
  699. * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting
  700. * ENOBUFS might not be good (it's not tunable per se), but otherwise
  701. * we don't have a good statistic (IpOutDiscards but it can be too many
  702. * things). We could add another new stat but at least for now that
  703. * seems like overkill.
  704. */
  705. if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
  706. UDP_INC_STATS_USER(sock_net(sk),
  707. UDP_MIB_SNDBUFERRORS, is_udplite);
  708. }
  709. return err;
  710. do_confirm:
  711. dst_confirm(&rt->u.dst);
  712. if (!(msg->msg_flags&MSG_PROBE) || len)
  713. goto back_from_confirm;
  714. err = 0;
  715. goto out;
  716. }
  717. EXPORT_SYMBOL(udp_sendmsg);
  718. int udp_sendpage(struct sock *sk, struct page *page, int offset,
  719. size_t size, int flags)
  720. {
  721. struct udp_sock *up = udp_sk(sk);
  722. int ret;
  723. if (!up->pending) {
  724. struct msghdr msg = { .msg_flags = flags|MSG_MORE };
  725. /* Call udp_sendmsg to specify destination address which
  726. * sendpage interface can't pass.
  727. * This will succeed only when the socket is connected.
  728. */
  729. ret = udp_sendmsg(NULL, sk, &msg, 0);
  730. if (ret < 0)
  731. return ret;
  732. }
  733. lock_sock(sk);
  734. if (unlikely(!up->pending)) {
  735. release_sock(sk);
  736. LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 3\n");
  737. return -EINVAL;
  738. }
  739. ret = ip_append_page(sk, page, offset, size, flags);
  740. if (ret == -EOPNOTSUPP) {
  741. release_sock(sk);
  742. return sock_no_sendpage(sk->sk_socket, page, offset,
  743. size, flags);
  744. }
  745. if (ret < 0) {
  746. udp_flush_pending_frames(sk);
  747. goto out;
  748. }
  749. up->len += size;
  750. if (!(up->corkflag || (flags&MSG_MORE)))
  751. ret = udp_push_pending_frames(sk);
  752. if (!ret)
  753. ret = size;
  754. out:
  755. release_sock(sk);
  756. return ret;
  757. }
  758. /**
  759. * first_packet_length - return length of first packet in receive queue
  760. * @sk: socket
  761. *
  762. * Drops all bad checksum frames, until a valid one is found.
  763. * Returns the length of found skb, or 0 if none is found.
  764. */
  765. static unsigned int first_packet_length(struct sock *sk)
  766. {
  767. struct sk_buff_head list_kill, *rcvq = &sk->sk_receive_queue;
  768. struct sk_buff *skb;
  769. unsigned int res;
  770. __skb_queue_head_init(&list_kill);
  771. spin_lock_bh(&rcvq->lock);
  772. while ((skb = skb_peek(rcvq)) != NULL &&
  773. udp_lib_checksum_complete(skb)) {
  774. UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS,
  775. IS_UDPLITE(sk));
  776. __skb_unlink(skb, rcvq);
  777. __skb_queue_tail(&list_kill, skb);
  778. }
  779. res = skb ? skb->len : 0;
  780. spin_unlock_bh(&rcvq->lock);
  781. if (!skb_queue_empty(&list_kill)) {
  782. lock_sock(sk);
  783. __skb_queue_purge(&list_kill);
  784. sk_mem_reclaim_partial(sk);
  785. release_sock(sk);
  786. }
  787. return res;
  788. }
  789. /*
  790. * IOCTL requests applicable to the UDP protocol
  791. */
  792. int udp_ioctl(struct sock *sk, int cmd, unsigned long arg)
  793. {
  794. switch (cmd) {
  795. case SIOCOUTQ:
  796. {
  797. int amount = sk_wmem_alloc_get(sk);
  798. return put_user(amount, (int __user *)arg);
  799. }
  800. case SIOCINQ:
  801. {
  802. unsigned int amount = first_packet_length(sk);
  803. if (amount)
  804. /*
  805. * We will only return the amount
  806. * of this packet since that is all
  807. * that will be read.
  808. */
  809. amount -= sizeof(struct udphdr);
  810. return put_user(amount, (int __user *)arg);
  811. }
  812. default:
  813. return -ENOIOCTLCMD;
  814. }
  815. return 0;
  816. }
  817. EXPORT_SYMBOL(udp_ioctl);
  818. /*
  819. * This should be easy, if there is something there we
  820. * return it, otherwise we block.
  821. */
  822. int udp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
  823. size_t len, int noblock, int flags, int *addr_len)
  824. {
  825. struct inet_sock *inet = inet_sk(sk);
  826. struct sockaddr_in *sin = (struct sockaddr_in *)msg->msg_name;
  827. struct sk_buff *skb;
  828. unsigned int ulen, copied;
  829. int peeked;
  830. int err;
  831. int is_udplite = IS_UDPLITE(sk);
  832. /*
  833. * Check any passed addresses
  834. */
  835. if (addr_len)
  836. *addr_len = sizeof(*sin);
  837. if (flags & MSG_ERRQUEUE)
  838. return ip_recv_error(sk, msg, len);
  839. try_again:
  840. skb = __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0),
  841. &peeked, &err);
  842. if (!skb)
  843. goto out;
  844. ulen = skb->len - sizeof(struct udphdr);
  845. copied = len;
  846. if (copied > ulen)
  847. copied = ulen;
  848. else if (copied < ulen)
  849. msg->msg_flags |= MSG_TRUNC;
  850. /*
  851. * If checksum is needed at all, try to do it while copying the
  852. * data. If the data is truncated, or if we only want a partial
  853. * coverage checksum (UDP-Lite), do it before the copy.
  854. */
  855. if (copied < ulen || UDP_SKB_CB(skb)->partial_cov) {
  856. if (udp_lib_checksum_complete(skb))
  857. goto csum_copy_err;
  858. }
  859. if (skb_csum_unnecessary(skb))
  860. err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr),
  861. msg->msg_iov, copied);
  862. else {
  863. err = skb_copy_and_csum_datagram_iovec(skb,
  864. sizeof(struct udphdr),
  865. msg->msg_iov);
  866. if (err == -EINVAL)
  867. goto csum_copy_err;
  868. }
  869. if (err)
  870. goto out_free;
  871. if (!peeked)
  872. UDP_INC_STATS_USER(sock_net(sk),
  873. UDP_MIB_INDATAGRAMS, is_udplite);
  874. sock_recv_timestamp(msg, sk, skb);
  875. /* Copy the address. */
  876. if (sin) {
  877. sin->sin_family = AF_INET;
  878. sin->sin_port = udp_hdr(skb)->source;
  879. sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
  880. memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
  881. }
  882. if (inet->cmsg_flags)
  883. ip_cmsg_recv(msg, skb);
  884. err = copied;
  885. if (flags & MSG_TRUNC)
  886. err = ulen;
  887. out_free:
  888. skb_free_datagram_locked(sk, skb);
  889. out:
  890. return err;
  891. csum_copy_err:
  892. lock_sock(sk);
  893. if (!skb_kill_datagram(sk, skb, flags))
  894. UDP_INC_STATS_USER(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
  895. release_sock(sk);
  896. if (noblock)
  897. return -EAGAIN;
  898. goto try_again;
  899. }
  900. int udp_disconnect(struct sock *sk, int flags)
  901. {
  902. struct inet_sock *inet = inet_sk(sk);
  903. /*
  904. * 1003.1g - break association.
  905. */
  906. sk->sk_state = TCP_CLOSE;
  907. inet->daddr = 0;
  908. inet->dport = 0;
  909. sk->sk_bound_dev_if = 0;
  910. if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK))
  911. inet_reset_saddr(sk);
  912. if (!(sk->sk_userlocks & SOCK_BINDPORT_LOCK)) {
  913. sk->sk_prot->unhash(sk);
  914. inet->sport = 0;
  915. }
  916. sk_dst_reset(sk);
  917. return 0;
  918. }
  919. EXPORT_SYMBOL(udp_disconnect);
  920. void udp_lib_unhash(struct sock *sk)
  921. {
  922. if (sk_hashed(sk)) {
  923. struct udp_table *udptable = sk->sk_prot->h.udp_table;
  924. unsigned int hash = udp_hashfn(sock_net(sk), sk->sk_hash);
  925. struct udp_hslot *hslot = &udptable->hash[hash];
  926. spin_lock_bh(&hslot->lock);
  927. if (sk_nulls_del_node_init_rcu(sk)) {
  928. inet_sk(sk)->num = 0;
  929. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
  930. }
  931. spin_unlock_bh(&hslot->lock);
  932. }
  933. }
  934. EXPORT_SYMBOL(udp_lib_unhash);
  935. static int __udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
  936. {
  937. int is_udplite = IS_UDPLITE(sk);
  938. int rc;
  939. if ((rc = sock_queue_rcv_skb(sk, skb)) < 0) {
  940. /* Note that an ENOMEM error is charged twice */
  941. if (rc == -ENOMEM) {
  942. UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_RCVBUFERRORS,
  943. is_udplite);
  944. atomic_inc(&sk->sk_drops);
  945. }
  946. goto drop;
  947. }
  948. return 0;
  949. drop:
  950. UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
  951. kfree_skb(skb);
  952. return -1;
  953. }
  954. /* returns:
  955. * -1: error
  956. * 0: success
  957. * >0: "udp encap" protocol resubmission
  958. *
  959. * Note that in the success and error cases, the skb is assumed to
  960. * have either been requeued or freed.
  961. */
  962. int udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
  963. {
  964. struct udp_sock *up = udp_sk(sk);
  965. int rc;
  966. int is_udplite = IS_UDPLITE(sk);
  967. /*
  968. * Charge it to the socket, dropping if the queue is full.
  969. */
  970. if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb))
  971. goto drop;
  972. nf_reset(skb);
  973. if (up->encap_type) {
  974. /*
  975. * This is an encapsulation socket so pass the skb to
  976. * the socket's udp_encap_rcv() hook. Otherwise, just
  977. * fall through and pass this up the UDP socket.
  978. * up->encap_rcv() returns the following value:
  979. * =0 if skb was successfully passed to the encap
  980. * handler or was discarded by it.
  981. * >0 if skb should be passed on to UDP.
  982. * <0 if skb should be resubmitted as proto -N
  983. */
  984. /* if we're overly short, let UDP handle it */
  985. if (skb->len > sizeof(struct udphdr) &&
  986. up->encap_rcv != NULL) {
  987. int ret;
  988. ret = (*up->encap_rcv)(sk, skb);
  989. if (ret <= 0) {
  990. UDP_INC_STATS_BH(sock_net(sk),
  991. UDP_MIB_INDATAGRAMS,
  992. is_udplite);
  993. return -ret;
  994. }
  995. }
  996. /* FALLTHROUGH -- it's a UDP Packet */
  997. }
  998. /*
  999. * UDP-Lite specific tests, ignored on UDP sockets
  1000. */
  1001. if ((is_udplite & UDPLITE_RECV_CC) && UDP_SKB_CB(skb)->partial_cov) {
  1002. /*
  1003. * MIB statistics other than incrementing the error count are
  1004. * disabled for the following two types of errors: these depend
  1005. * on the application settings, not on the functioning of the
  1006. * protocol stack as such.
  1007. *
  1008. * RFC 3828 here recommends (sec 3.3): "There should also be a
  1009. * way ... to ... at least let the receiving application block
  1010. * delivery of packets with coverage values less than a value
  1011. * provided by the application."
  1012. */
  1013. if (up->pcrlen == 0) { /* full coverage was set */
  1014. LIMIT_NETDEBUG(KERN_WARNING "UDPLITE: partial coverage "
  1015. "%d while full coverage %d requested\n",
  1016. UDP_SKB_CB(skb)->cscov, skb->len);
  1017. goto drop;
  1018. }
  1019. /* The next case involves violating the min. coverage requested
  1020. * by the receiver. This is subtle: if receiver wants x and x is
  1021. * greater than the buffersize/MTU then receiver will complain
  1022. * that it wants x while sender emits packets of smaller size y.
  1023. * Therefore the above ...()->partial_cov statement is essential.
  1024. */
  1025. if (UDP_SKB_CB(skb)->cscov < up->pcrlen) {
  1026. LIMIT_NETDEBUG(KERN_WARNING
  1027. "UDPLITE: coverage %d too small, need min %d\n",
  1028. UDP_SKB_CB(skb)->cscov, up->pcrlen);
  1029. goto drop;
  1030. }
  1031. }
  1032. if (sk->sk_filter) {
  1033. if (udp_lib_checksum_complete(skb))
  1034. goto drop;
  1035. }
  1036. rc = 0;
  1037. bh_lock_sock(sk);
  1038. if (!sock_owned_by_user(sk))
  1039. rc = __udp_queue_rcv_skb(sk, skb);
  1040. else
  1041. sk_add_backlog(sk, skb);
  1042. bh_unlock_sock(sk);
  1043. return rc;
  1044. drop:
  1045. UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
  1046. kfree_skb(skb);
  1047. return -1;
  1048. }
  1049. /*
  1050. * Multicasts and broadcasts go to each listener.
  1051. *
  1052. * Note: called only from the BH handler context,
  1053. * so we don't need to lock the hashes.
  1054. */
  1055. static int __udp4_lib_mcast_deliver(struct net *net, struct sk_buff *skb,
  1056. struct udphdr *uh,
  1057. __be32 saddr, __be32 daddr,
  1058. struct udp_table *udptable)
  1059. {
  1060. struct sock *sk;
  1061. struct udp_hslot *hslot = &udptable->hash[udp_hashfn(net, ntohs(uh->dest))];
  1062. int dif;
  1063. spin_lock(&hslot->lock);
  1064. sk = sk_nulls_head(&hslot->head);
  1065. dif = skb->dev->ifindex;
  1066. sk = udp_v4_mcast_next(net, sk, uh->dest, daddr, uh->source, saddr, dif);
  1067. if (sk) {
  1068. struct sock *sknext = NULL;
  1069. do {
  1070. struct sk_buff *skb1 = skb;
  1071. sknext = udp_v4_mcast_next(net, sk_nulls_next(sk), uh->dest,
  1072. daddr, uh->source, saddr,
  1073. dif);
  1074. if (sknext)
  1075. skb1 = skb_clone(skb, GFP_ATOMIC);
  1076. if (skb1) {
  1077. int ret = udp_queue_rcv_skb(sk, skb1);
  1078. if (ret > 0)
  1079. /* we should probably re-process instead
  1080. * of dropping packets here. */
  1081. kfree_skb(skb1);
  1082. }
  1083. sk = sknext;
  1084. } while (sknext);
  1085. } else
  1086. consume_skb(skb);
  1087. spin_unlock(&hslot->lock);
  1088. return 0;
  1089. }
  1090. /* Initialize UDP checksum. If exited with zero value (success),
  1091. * CHECKSUM_UNNECESSARY means, that no more checks are required.
  1092. * Otherwise, csum completion requires chacksumming packet body,
  1093. * including udp header and folding it to skb->csum.
  1094. */
  1095. static inline int udp4_csum_init(struct sk_buff *skb, struct udphdr *uh,
  1096. int proto)
  1097. {
  1098. const struct iphdr *iph;
  1099. int err;
  1100. UDP_SKB_CB(skb)->partial_cov = 0;
  1101. UDP_SKB_CB(skb)->cscov = skb->len;
  1102. if (proto == IPPROTO_UDPLITE) {
  1103. err = udplite_checksum_init(skb, uh);
  1104. if (err)
  1105. return err;
  1106. }
  1107. iph = ip_hdr(skb);
  1108. if (uh->check == 0) {
  1109. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1110. } else if (skb->ip_summed == CHECKSUM_COMPLETE) {
  1111. if (!csum_tcpudp_magic(iph->saddr, iph->daddr, skb->len,
  1112. proto, skb->csum))
  1113. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1114. }
  1115. if (!skb_csum_unnecessary(skb))
  1116. skb->csum = csum_tcpudp_nofold(iph->saddr, iph->daddr,
  1117. skb->len, proto, 0);
  1118. /* Probably, we should checksum udp header (it should be in cache
  1119. * in any case) and data in tiny packets (< rx copybreak).
  1120. */
  1121. return 0;
  1122. }
  1123. /*
  1124. * All we need to do is get the socket, and then do a checksum.
  1125. */
  1126. int __udp4_lib_rcv(struct sk_buff *skb, struct udp_table *udptable,
  1127. int proto)
  1128. {
  1129. struct sock *sk;
  1130. struct udphdr *uh;
  1131. unsigned short ulen;
  1132. struct rtable *rt = skb_rtable(skb);
  1133. __be32 saddr, daddr;
  1134. struct net *net = dev_net(skb->dev);
  1135. /*
  1136. * Validate the packet.
  1137. */
  1138. if (!pskb_may_pull(skb, sizeof(struct udphdr)))
  1139. goto drop; /* No space for header. */
  1140. uh = udp_hdr(skb);
  1141. ulen = ntohs(uh->len);
  1142. if (ulen > skb->len)
  1143. goto short_packet;
  1144. if (proto == IPPROTO_UDP) {
  1145. /* UDP validates ulen. */
  1146. if (ulen < sizeof(*uh) || pskb_trim_rcsum(skb, ulen))
  1147. goto short_packet;
  1148. uh = udp_hdr(skb);
  1149. }
  1150. if (udp4_csum_init(skb, uh, proto))
  1151. goto csum_error;
  1152. saddr = ip_hdr(skb)->saddr;
  1153. daddr = ip_hdr(skb)->daddr;
  1154. if (rt->rt_flags & (RTCF_BROADCAST|RTCF_MULTICAST))
  1155. return __udp4_lib_mcast_deliver(net, skb, uh,
  1156. saddr, daddr, udptable);
  1157. sk = __udp4_lib_lookup_skb(skb, uh->source, uh->dest, udptable);
  1158. if (sk != NULL) {
  1159. int ret = udp_queue_rcv_skb(sk, skb);
  1160. sock_put(sk);
  1161. /* a return value > 0 means to resubmit the input, but
  1162. * it wants the return to be -protocol, or 0
  1163. */
  1164. if (ret > 0)
  1165. return -ret;
  1166. return 0;
  1167. }
  1168. if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
  1169. goto drop;
  1170. nf_reset(skb);
  1171. /* No socket. Drop packet silently, if checksum is wrong */
  1172. if (udp_lib_checksum_complete(skb))
  1173. goto csum_error;
  1174. UDP_INC_STATS_BH(net, UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE);
  1175. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
  1176. /*
  1177. * Hmm. We got an UDP packet to a port to which we
  1178. * don't wanna listen. Ignore it.
  1179. */
  1180. kfree_skb(skb);
  1181. return 0;
  1182. short_packet:
  1183. LIMIT_NETDEBUG(KERN_DEBUG "UDP%s: short packet: From %pI4:%u %d/%d to %pI4:%u\n",
  1184. proto == IPPROTO_UDPLITE ? "-Lite" : "",
  1185. &saddr,
  1186. ntohs(uh->source),
  1187. ulen,
  1188. skb->len,
  1189. &daddr,
  1190. ntohs(uh->dest));
  1191. goto drop;
  1192. csum_error:
  1193. /*
  1194. * RFC1122: OK. Discards the bad packet silently (as far as
  1195. * the network is concerned, anyway) as per 4.1.3.4 (MUST).
  1196. */
  1197. LIMIT_NETDEBUG(KERN_DEBUG "UDP%s: bad checksum. From %pI4:%u to %pI4:%u ulen %d\n",
  1198. proto == IPPROTO_UDPLITE ? "-Lite" : "",
  1199. &saddr,
  1200. ntohs(uh->source),
  1201. &daddr,
  1202. ntohs(uh->dest),
  1203. ulen);
  1204. drop:
  1205. UDP_INC_STATS_BH(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE);
  1206. kfree_skb(skb);
  1207. return 0;
  1208. }
  1209. int udp_rcv(struct sk_buff *skb)
  1210. {
  1211. return __udp4_lib_rcv(skb, &udp_table, IPPROTO_UDP);
  1212. }
  1213. void udp_destroy_sock(struct sock *sk)
  1214. {
  1215. lock_sock(sk);
  1216. udp_flush_pending_frames(sk);
  1217. release_sock(sk);
  1218. }
  1219. /*
  1220. * Socket option code for UDP
  1221. */
  1222. int udp_lib_setsockopt(struct sock *sk, int level, int optname,
  1223. char __user *optval, unsigned int optlen,
  1224. int (*push_pending_frames)(struct sock *))
  1225. {
  1226. struct udp_sock *up = udp_sk(sk);
  1227. int val;
  1228. int err = 0;
  1229. int is_udplite = IS_UDPLITE(sk);
  1230. if (optlen < sizeof(int))
  1231. return -EINVAL;
  1232. if (get_user(val, (int __user *)optval))
  1233. return -EFAULT;
  1234. switch (optname) {
  1235. case UDP_CORK:
  1236. if (val != 0) {
  1237. up->corkflag = 1;
  1238. } else {
  1239. up->corkflag = 0;
  1240. lock_sock(sk);
  1241. (*push_pending_frames)(sk);
  1242. release_sock(sk);
  1243. }
  1244. break;
  1245. case UDP_ENCAP:
  1246. switch (val) {
  1247. case 0:
  1248. case UDP_ENCAP_ESPINUDP:
  1249. case UDP_ENCAP_ESPINUDP_NON_IKE:
  1250. up->encap_rcv = xfrm4_udp_encap_rcv;
  1251. /* FALLTHROUGH */
  1252. case UDP_ENCAP_L2TPINUDP:
  1253. up->encap_type = val;
  1254. break;
  1255. default:
  1256. err = -ENOPROTOOPT;
  1257. break;
  1258. }
  1259. break;
  1260. /*
  1261. * UDP-Lite's partial checksum coverage (RFC 3828).
  1262. */
  1263. /* The sender sets actual checksum coverage length via this option.
  1264. * The case coverage > packet length is handled by send module. */
  1265. case UDPLITE_SEND_CSCOV:
  1266. if (!is_udplite) /* Disable the option on UDP sockets */
  1267. return -ENOPROTOOPT;
  1268. if (val != 0 && val < 8) /* Illegal coverage: use default (8) */
  1269. val = 8;
  1270. else if (val > USHORT_MAX)
  1271. val = USHORT_MAX;
  1272. up->pcslen = val;
  1273. up->pcflag |= UDPLITE_SEND_CC;
  1274. break;
  1275. /* The receiver specifies a minimum checksum coverage value. To make
  1276. * sense, this should be set to at least 8 (as done below). If zero is
  1277. * used, this again means full checksum coverage. */
  1278. case UDPLITE_RECV_CSCOV:
  1279. if (!is_udplite) /* Disable the option on UDP sockets */
  1280. return -ENOPROTOOPT;
  1281. if (val != 0 && val < 8) /* Avoid silly minimal values. */
  1282. val = 8;
  1283. else if (val > USHORT_MAX)
  1284. val = USHORT_MAX;
  1285. up->pcrlen = val;
  1286. up->pcflag |= UDPLITE_RECV_CC;
  1287. break;
  1288. default:
  1289. err = -ENOPROTOOPT;
  1290. break;
  1291. }
  1292. return err;
  1293. }
  1294. EXPORT_SYMBOL(udp_lib_setsockopt);
  1295. int udp_setsockopt(struct sock *sk, int level, int optname,
  1296. char __user *optval, unsigned int optlen)
  1297. {
  1298. if (level == SOL_UDP || level == SOL_UDPLITE)
  1299. return udp_lib_setsockopt(sk, level, optname, optval, optlen,
  1300. udp_push_pending_frames);
  1301. return ip_setsockopt(sk, level, optname, optval, optlen);
  1302. }
  1303. #ifdef CONFIG_COMPAT
  1304. int compat_udp_setsockopt(struct sock *sk, int level, int optname,
  1305. char __user *optval, unsigned int optlen)
  1306. {
  1307. if (level == SOL_UDP || level == SOL_UDPLITE)
  1308. return udp_lib_setsockopt(sk, level, optname, optval, optlen,
  1309. udp_push_pending_frames);
  1310. return compat_ip_setsockopt(sk, level, optname, optval, optlen);
  1311. }
  1312. #endif
  1313. int udp_lib_getsockopt(struct sock *sk, int level, int optname,
  1314. char __user *optval, int __user *optlen)
  1315. {
  1316. struct udp_sock *up = udp_sk(sk);
  1317. int val, len;
  1318. if (get_user(len, optlen))
  1319. return -EFAULT;
  1320. len = min_t(unsigned int, len, sizeof(int));
  1321. if (len < 0)
  1322. return -EINVAL;
  1323. switch (optname) {
  1324. case UDP_CORK:
  1325. val = up->corkflag;
  1326. break;
  1327. case UDP_ENCAP:
  1328. val = up->encap_type;
  1329. break;
  1330. /* The following two cannot be changed on UDP sockets, the return is
  1331. * always 0 (which corresponds to the full checksum coverage of UDP). */
  1332. case UDPLITE_SEND_CSCOV:
  1333. val = up->pcslen;
  1334. break;
  1335. case UDPLITE_RECV_CSCOV:
  1336. val = up->pcrlen;
  1337. break;
  1338. default:
  1339. return -ENOPROTOOPT;
  1340. }
  1341. if (put_user(len, optlen))
  1342. return -EFAULT;
  1343. if (copy_to_user(optval, &val, len))
  1344. return -EFAULT;
  1345. return 0;
  1346. }
  1347. EXPORT_SYMBOL(udp_lib_getsockopt);
  1348. int udp_getsockopt(struct sock *sk, int level, int optname,
  1349. char __user *optval, int __user *optlen)
  1350. {
  1351. if (level == SOL_UDP || level == SOL_UDPLITE)
  1352. return udp_lib_getsockopt(sk, level, optname, optval, optlen);
  1353. return ip_getsockopt(sk, level, optname, optval, optlen);
  1354. }
  1355. #ifdef CONFIG_COMPAT
  1356. int compat_udp_getsockopt(struct sock *sk, int level, int optname,
  1357. char __user *optval, int __user *optlen)
  1358. {
  1359. if (level == SOL_UDP || level == SOL_UDPLITE)
  1360. return udp_lib_getsockopt(sk, level, optname, optval, optlen);
  1361. return compat_ip_getsockopt(sk, level, optname, optval, optlen);
  1362. }
  1363. #endif
  1364. /**
  1365. * udp_poll - wait for a UDP event.
  1366. * @file - file struct
  1367. * @sock - socket
  1368. * @wait - poll table
  1369. *
  1370. * This is same as datagram poll, except for the special case of
  1371. * blocking sockets. If application is using a blocking fd
  1372. * and a packet with checksum error is in the queue;
  1373. * then it could get return from select indicating data available
  1374. * but then block when reading it. Add special case code
  1375. * to work around these arguably broken applications.
  1376. */
  1377. unsigned int udp_poll(struct file *file, struct socket *sock, poll_table *wait)
  1378. {
  1379. unsigned int mask = datagram_poll(file, sock, wait);
  1380. struct sock *sk = sock->sk;
  1381. /* Check for false positives due to checksum errors */
  1382. if ((mask & POLLRDNORM) && !(file->f_flags & O_NONBLOCK) &&
  1383. !(sk->sk_shutdown & RCV_SHUTDOWN) && !first_packet_length(sk))
  1384. mask &= ~(POLLIN | POLLRDNORM);
  1385. return mask;
  1386. }
  1387. EXPORT_SYMBOL(udp_poll);
  1388. struct proto udp_prot = {
  1389. .name = "UDP",
  1390. .owner = THIS_MODULE,
  1391. .close = udp_lib_close,
  1392. .connect = ip4_datagram_connect,
  1393. .disconnect = udp_disconnect,
  1394. .ioctl = udp_ioctl,
  1395. .destroy = udp_destroy_sock,
  1396. .setsockopt = udp_setsockopt,
  1397. .getsockopt = udp_getsockopt,
  1398. .sendmsg = udp_sendmsg,
  1399. .recvmsg = udp_recvmsg,
  1400. .sendpage = udp_sendpage,
  1401. .backlog_rcv = __udp_queue_rcv_skb,
  1402. .hash = udp_lib_hash,
  1403. .unhash = udp_lib_unhash,
  1404. .get_port = udp_v4_get_port,
  1405. .memory_allocated = &udp_memory_allocated,
  1406. .sysctl_mem = sysctl_udp_mem,
  1407. .sysctl_wmem = &sysctl_udp_wmem_min,
  1408. .sysctl_rmem = &sysctl_udp_rmem_min,
  1409. .obj_size = sizeof(struct udp_sock),
  1410. .slab_flags = SLAB_DESTROY_BY_RCU,
  1411. .h.udp_table = &udp_table,
  1412. #ifdef CONFIG_COMPAT
  1413. .compat_setsockopt = compat_udp_setsockopt,
  1414. .compat_getsockopt = compat_udp_getsockopt,
  1415. #endif
  1416. };
  1417. EXPORT_SYMBOL(udp_prot);
  1418. /* ------------------------------------------------------------------------ */
  1419. #ifdef CONFIG_PROC_FS
  1420. static struct sock *udp_get_first(struct seq_file *seq, int start)
  1421. {
  1422. struct sock *sk;
  1423. struct udp_iter_state *state = seq->private;
  1424. struct net *net = seq_file_net(seq);
  1425. for (state->bucket = start; state->bucket < UDP_HTABLE_SIZE; ++state->bucket) {
  1426. struct hlist_nulls_node *node;
  1427. struct udp_hslot *hslot = &state->udp_table->hash[state->bucket];
  1428. spin_lock_bh(&hslot->lock);
  1429. sk_nulls_for_each(sk, node, &hslot->head) {
  1430. if (!net_eq(sock_net(sk), net))
  1431. continue;
  1432. if (sk->sk_family == state->family)
  1433. goto found;
  1434. }
  1435. spin_unlock_bh(&hslot->lock);
  1436. }
  1437. sk = NULL;
  1438. found:
  1439. return sk;
  1440. }
  1441. static struct sock *udp_get_next(struct seq_file *seq, struct sock *sk)
  1442. {
  1443. struct udp_iter_state *state = seq->private;
  1444. struct net *net = seq_file_net(seq);
  1445. do {
  1446. sk = sk_nulls_next(sk);
  1447. } while (sk && (!net_eq(sock_net(sk), net) || sk->sk_family != state->family));
  1448. if (!sk) {
  1449. if (state->bucket < UDP_HTABLE_SIZE)
  1450. spin_unlock_bh(&state->udp_table->hash[state->bucket].lock);
  1451. return udp_get_first(seq, state->bucket + 1);
  1452. }
  1453. return sk;
  1454. }
  1455. static struct sock *udp_get_idx(struct seq_file *seq, loff_t pos)
  1456. {
  1457. struct sock *sk = udp_get_first(seq, 0);
  1458. if (sk)
  1459. while (pos && (sk = udp_get_next(seq, sk)) != NULL)
  1460. --pos;
  1461. return pos ? NULL : sk;
  1462. }
  1463. static void *udp_seq_start(struct seq_file *seq, loff_t *pos)
  1464. {
  1465. struct udp_iter_state *state = seq->private;
  1466. state->bucket = UDP_HTABLE_SIZE;
  1467. return *pos ? udp_get_idx(seq, *pos-1) : SEQ_START_TOKEN;
  1468. }
  1469. static void *udp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  1470. {
  1471. struct sock *sk;
  1472. if (v == SEQ_START_TOKEN)
  1473. sk = udp_get_idx(seq, 0);
  1474. else
  1475. sk = udp_get_next(seq, v);
  1476. ++*pos;
  1477. return sk;
  1478. }
  1479. static void udp_seq_stop(struct seq_file *seq, void *v)
  1480. {
  1481. struct udp_iter_state *state = seq->private;
  1482. if (state->bucket < UDP_HTABLE_SIZE)
  1483. spin_unlock_bh(&state->udp_table->hash[state->bucket].lock);
  1484. }
  1485. static int udp_seq_open(struct inode *inode, struct file *file)
  1486. {
  1487. struct udp_seq_afinfo *afinfo = PDE(inode)->data;
  1488. struct udp_iter_state *s;
  1489. int err;
  1490. err = seq_open_net(inode, file, &afinfo->seq_ops,
  1491. sizeof(struct udp_iter_state));
  1492. if (err < 0)
  1493. return err;
  1494. s = ((struct seq_file *)file->private_data)->private;
  1495. s->family = afinfo->family;
  1496. s->udp_table = afinfo->udp_table;
  1497. return err;
  1498. }
  1499. /* ------------------------------------------------------------------------ */
  1500. int udp_proc_register(struct net *net, struct udp_seq_afinfo *afinfo)
  1501. {
  1502. struct proc_dir_entry *p;
  1503. int rc = 0;
  1504. afinfo->seq_fops.open = udp_seq_open;
  1505. afinfo->seq_fops.read = seq_read;
  1506. afinfo->seq_fops.llseek = seq_lseek;
  1507. afinfo->seq_fops.release = seq_release_net;
  1508. afinfo->seq_ops.start = udp_seq_start;
  1509. afinfo->seq_ops.next = udp_seq_next;
  1510. afinfo->seq_ops.stop = udp_seq_stop;
  1511. p = proc_create_data(afinfo->name, S_IRUGO, net->proc_net,
  1512. &afinfo->seq_fops, afinfo);
  1513. if (!p)
  1514. rc = -ENOMEM;
  1515. return rc;
  1516. }
  1517. EXPORT_SYMBOL(udp_proc_register);
  1518. void udp_proc_unregister(struct net *net, struct udp_seq_afinfo *afinfo)
  1519. {
  1520. proc_net_remove(net, afinfo->name);
  1521. }
  1522. EXPORT_SYMBOL(udp_proc_unregister);
  1523. /* ------------------------------------------------------------------------ */
  1524. static void udp4_format_sock(struct sock *sp, struct seq_file *f,
  1525. int bucket, int *len)
  1526. {
  1527. struct inet_sock *inet = inet_sk(sp);
  1528. __be32 dest = inet->daddr;
  1529. __be32 src = inet->rcv_saddr;
  1530. __u16 destp = ntohs(inet->dport);
  1531. __u16 srcp = ntohs(inet->sport);
  1532. seq_printf(f, "%4d: %08X:%04X %08X:%04X"
  1533. " %02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p %d%n",
  1534. bucket, src, srcp, dest, destp, sp->sk_state,
  1535. sk_wmem_alloc_get(sp),
  1536. sk_rmem_alloc_get(sp),
  1537. 0, 0L, 0, sock_i_uid(sp), 0, sock_i_ino(sp),
  1538. atomic_read(&sp->sk_refcnt), sp,
  1539. atomic_read(&sp->sk_drops), len);
  1540. }
  1541. int udp4_seq_show(struct seq_file *seq, void *v)
  1542. {
  1543. if (v == SEQ_START_TOKEN)
  1544. seq_printf(seq, "%-127s\n",
  1545. " sl local_address rem_address st tx_queue "
  1546. "rx_queue tr tm->when retrnsmt uid timeout "
  1547. "inode ref pointer drops");
  1548. else {
  1549. struct udp_iter_state *state = seq->private;
  1550. int len;
  1551. udp4_format_sock(v, seq, state->bucket, &len);
  1552. seq_printf(seq, "%*s\n", 127 - len, "");
  1553. }
  1554. return 0;
  1555. }
  1556. /* ------------------------------------------------------------------------ */
  1557. static struct udp_seq_afinfo udp4_seq_afinfo = {
  1558. .name = "udp",
  1559. .family = AF_INET,
  1560. .udp_table = &udp_table,
  1561. .seq_fops = {
  1562. .owner = THIS_MODULE,
  1563. },
  1564. .seq_ops = {
  1565. .show = udp4_seq_show,
  1566. },
  1567. };
  1568. static int udp4_proc_init_net(struct net *net)
  1569. {
  1570. return udp_proc_register(net, &udp4_seq_afinfo);
  1571. }
  1572. static void udp4_proc_exit_net(struct net *net)
  1573. {
  1574. udp_proc_unregister(net, &udp4_seq_afinfo);
  1575. }
  1576. static struct pernet_operations udp4_net_ops = {
  1577. .init = udp4_proc_init_net,
  1578. .exit = udp4_proc_exit_net,
  1579. };
  1580. int __init udp4_proc_init(void)
  1581. {
  1582. return register_pernet_subsys(&udp4_net_ops);
  1583. }
  1584. void udp4_proc_exit(void)
  1585. {
  1586. unregister_pernet_subsys(&udp4_net_ops);
  1587. }
  1588. #endif /* CONFIG_PROC_FS */
  1589. void __init udp_table_init(struct udp_table *table)
  1590. {
  1591. int i;
  1592. for (i = 0; i < UDP_HTABLE_SIZE; i++) {
  1593. INIT_HLIST_NULLS_HEAD(&table->hash[i].head, i);
  1594. spin_lock_init(&table->hash[i].lock);
  1595. }
  1596. }
  1597. void __init udp_init(void)
  1598. {
  1599. unsigned long nr_pages, limit;
  1600. udp_table_init(&udp_table);
  1601. /* Set the pressure threshold up by the same strategy of TCP. It is a
  1602. * fraction of global memory that is up to 1/2 at 256 MB, decreasing
  1603. * toward zero with the amount of memory, with a floor of 128 pages.
  1604. */
  1605. nr_pages = totalram_pages - totalhigh_pages;
  1606. limit = min(nr_pages, 1UL<<(28-PAGE_SHIFT)) >> (20-PAGE_SHIFT);
  1607. limit = (limit * (nr_pages >> (20-PAGE_SHIFT))) >> (PAGE_SHIFT-11);
  1608. limit = max(limit, 128UL);
  1609. sysctl_udp_mem[0] = limit / 4 * 3;
  1610. sysctl_udp_mem[1] = limit;
  1611. sysctl_udp_mem[2] = sysctl_udp_mem[0] * 2;
  1612. sysctl_udp_rmem_min = SK_MEM_QUANTUM;
  1613. sysctl_udp_wmem_min = SK_MEM_QUANTUM;
  1614. }
  1615. int udp4_ufo_send_check(struct sk_buff *skb)
  1616. {
  1617. const struct iphdr *iph;
  1618. struct udphdr *uh;
  1619. if (!pskb_may_pull(skb, sizeof(*uh)))
  1620. return -EINVAL;
  1621. iph = ip_hdr(skb);
  1622. uh = udp_hdr(skb);
  1623. uh->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr, skb->len,
  1624. IPPROTO_UDP, 0);
  1625. skb->csum_start = skb_transport_header(skb) - skb->head;
  1626. skb->csum_offset = offsetof(struct udphdr, check);
  1627. skb->ip_summed = CHECKSUM_PARTIAL;
  1628. return 0;
  1629. }
  1630. struct sk_buff *udp4_ufo_fragment(struct sk_buff *skb, int features)
  1631. {
  1632. struct sk_buff *segs = ERR_PTR(-EINVAL);
  1633. unsigned int mss;
  1634. int offset;
  1635. __wsum csum;
  1636. mss = skb_shinfo(skb)->gso_size;
  1637. if (unlikely(skb->len <= mss))
  1638. goto out;
  1639. if (skb_gso_ok(skb, features | NETIF_F_GSO_ROBUST)) {
  1640. /* Packet is from an untrusted source, reset gso_segs. */
  1641. int type = skb_shinfo(skb)->gso_type;
  1642. if (unlikely(type & ~(SKB_GSO_UDP | SKB_GSO_DODGY) ||
  1643. !(type & (SKB_GSO_UDP))))
  1644. goto out;
  1645. skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(skb->len, mss);
  1646. segs = NULL;
  1647. goto out;
  1648. }
  1649. /* Do software UFO. Complete and fill in the UDP checksum as HW cannot
  1650. * do checksum of UDP packets sent as multiple IP fragments.
  1651. */
  1652. offset = skb->csum_start - skb_headroom(skb);
  1653. csum = skb_checksum(skb, offset, skb->len - offset, 0);
  1654. offset += skb->csum_offset;
  1655. *(__sum16 *)(skb->data + offset) = csum_fold(csum);
  1656. skb->ip_summed = CHECKSUM_NONE;
  1657. /* Fragment the skb. IP headers of the fragments are updated in
  1658. * inet_gso_segment()
  1659. */
  1660. segs = skb_segment(skb, features);
  1661. out:
  1662. return segs;
  1663. }