udp.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939
  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 __read_mostly;
  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 MAX_UDP_PORTS 65536
  118. #define PORTS_PER_CHAIN (MAX_UDP_PORTS / UDP_HTABLE_SIZE_MIN)
  119. static int udp_lib_lport_inuse(struct net *net, __u16 num,
  120. const struct udp_hslot *hslot,
  121. unsigned long *bitmap,
  122. struct sock *sk,
  123. int (*saddr_comp)(const struct sock *sk1,
  124. const struct sock *sk2),
  125. unsigned int log)
  126. {
  127. struct sock *sk2;
  128. struct hlist_nulls_node *node;
  129. sk_nulls_for_each(sk2, node, &hslot->head)
  130. if (net_eq(sock_net(sk2), net) &&
  131. sk2 != sk &&
  132. (bitmap || sk2->sk_hash == num) &&
  133. (!sk2->sk_reuse || !sk->sk_reuse) &&
  134. (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if
  135. || sk2->sk_bound_dev_if == sk->sk_bound_dev_if) &&
  136. (*saddr_comp)(sk, sk2)) {
  137. if (bitmap)
  138. __set_bit(sk2->sk_hash >> log, bitmap);
  139. else
  140. return 1;
  141. }
  142. return 0;
  143. }
  144. /**
  145. * udp_lib_get_port - UDP/-Lite port lookup for IPv4 and IPv6
  146. *
  147. * @sk: socket struct in question
  148. * @snum: port number to look up
  149. * @saddr_comp: AF-dependent comparison of bound local IP addresses
  150. */
  151. int udp_lib_get_port(struct sock *sk, unsigned short snum,
  152. int (*saddr_comp)(const struct sock *sk1,
  153. const struct sock *sk2))
  154. {
  155. struct udp_hslot *hslot;
  156. struct udp_table *udptable = sk->sk_prot->h.udp_table;
  157. int error = 1;
  158. struct net *net = sock_net(sk);
  159. if (!snum) {
  160. int low, high, remaining;
  161. unsigned rand;
  162. unsigned short first, last;
  163. DECLARE_BITMAP(bitmap, PORTS_PER_CHAIN);
  164. inet_get_local_port_range(&low, &high);
  165. remaining = (high - low) + 1;
  166. rand = net_random();
  167. first = (((u64)rand * remaining) >> 32) + low;
  168. /*
  169. * force rand to be an odd multiple of UDP_HTABLE_SIZE
  170. */
  171. rand = (rand | 1) * (udptable->mask + 1);
  172. for (last = first + udptable->mask + 1;
  173. first != last;
  174. first++) {
  175. hslot = udp_hashslot(udptable, net, first);
  176. bitmap_zero(bitmap, PORTS_PER_CHAIN);
  177. spin_lock_bh(&hslot->lock);
  178. udp_lib_lport_inuse(net, snum, hslot, bitmap, sk,
  179. saddr_comp, udptable->log);
  180. snum = first;
  181. /*
  182. * Iterate on all possible values of snum for this hash.
  183. * Using steps of an odd multiple of UDP_HTABLE_SIZE
  184. * give us randomization and full range coverage.
  185. */
  186. do {
  187. if (low <= snum && snum <= high &&
  188. !test_bit(snum >> udptable->log, bitmap))
  189. goto found;
  190. snum += rand;
  191. } while (snum != first);
  192. spin_unlock_bh(&hslot->lock);
  193. }
  194. goto fail;
  195. } else {
  196. hslot = udp_hashslot(udptable, net, snum);
  197. spin_lock_bh(&hslot->lock);
  198. if (udp_lib_lport_inuse(net, snum, hslot, NULL, sk,
  199. saddr_comp, 0))
  200. goto fail_unlock;
  201. }
  202. found:
  203. inet_sk(sk)->num = snum;
  204. sk->sk_hash = snum;
  205. if (sk_unhashed(sk)) {
  206. sk_nulls_add_node_rcu(sk, &hslot->head);
  207. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
  208. }
  209. error = 0;
  210. fail_unlock:
  211. spin_unlock_bh(&hslot->lock);
  212. fail:
  213. return error;
  214. }
  215. EXPORT_SYMBOL(udp_lib_get_port);
  216. static int ipv4_rcv_saddr_equal(const struct sock *sk1, const struct sock *sk2)
  217. {
  218. struct inet_sock *inet1 = inet_sk(sk1), *inet2 = inet_sk(sk2);
  219. return (!ipv6_only_sock(sk2) &&
  220. (!inet1->rcv_saddr || !inet2->rcv_saddr ||
  221. inet1->rcv_saddr == inet2->rcv_saddr));
  222. }
  223. int udp_v4_get_port(struct sock *sk, unsigned short snum)
  224. {
  225. return udp_lib_get_port(sk, snum, ipv4_rcv_saddr_equal);
  226. }
  227. static inline int compute_score(struct sock *sk, struct net *net, __be32 saddr,
  228. unsigned short hnum,
  229. __be16 sport, __be32 daddr, __be16 dport, int dif)
  230. {
  231. int score = -1;
  232. if (net_eq(sock_net(sk), net) && sk->sk_hash == hnum &&
  233. !ipv6_only_sock(sk)) {
  234. struct inet_sock *inet = inet_sk(sk);
  235. score = (sk->sk_family == PF_INET ? 1 : 0);
  236. if (inet->rcv_saddr) {
  237. if (inet->rcv_saddr != daddr)
  238. return -1;
  239. score += 2;
  240. }
  241. if (inet->daddr) {
  242. if (inet->daddr != saddr)
  243. return -1;
  244. score += 2;
  245. }
  246. if (inet->dport) {
  247. if (inet->dport != sport)
  248. return -1;
  249. score += 2;
  250. }
  251. if (sk->sk_bound_dev_if) {
  252. if (sk->sk_bound_dev_if != dif)
  253. return -1;
  254. score += 2;
  255. }
  256. }
  257. return score;
  258. }
  259. /* UDP is nearly always wildcards out the wazoo, it makes no sense to try
  260. * harder than this. -DaveM
  261. */
  262. static struct sock *__udp4_lib_lookup(struct net *net, __be32 saddr,
  263. __be16 sport, __be32 daddr, __be16 dport,
  264. int dif, struct udp_table *udptable)
  265. {
  266. struct sock *sk, *result;
  267. struct hlist_nulls_node *node;
  268. unsigned short hnum = ntohs(dport);
  269. unsigned int hash = udp_hashfn(net, hnum, udptable->mask);
  270. struct udp_hslot *hslot = &udptable->hash[hash];
  271. int score, badness;
  272. rcu_read_lock();
  273. begin:
  274. result = NULL;
  275. badness = -1;
  276. sk_nulls_for_each_rcu(sk, node, &hslot->head) {
  277. score = compute_score(sk, net, saddr, hnum, sport,
  278. daddr, dport, dif);
  279. if (score > badness) {
  280. result = sk;
  281. badness = score;
  282. }
  283. }
  284. /*
  285. * if the nulls value we got at the end of this lookup is
  286. * not the expected one, we must restart lookup.
  287. * We probably met an item that was moved to another chain.
  288. */
  289. if (get_nulls_value(node) != hash)
  290. goto begin;
  291. if (result) {
  292. if (unlikely(!atomic_inc_not_zero(&result->sk_refcnt)))
  293. result = NULL;
  294. else if (unlikely(compute_score(result, net, saddr, hnum, sport,
  295. daddr, dport, dif) < badness)) {
  296. sock_put(result);
  297. goto begin;
  298. }
  299. }
  300. rcu_read_unlock();
  301. return result;
  302. }
  303. static inline struct sock *__udp4_lib_lookup_skb(struct sk_buff *skb,
  304. __be16 sport, __be16 dport,
  305. struct udp_table *udptable)
  306. {
  307. struct sock *sk;
  308. const struct iphdr *iph = ip_hdr(skb);
  309. if (unlikely(sk = skb_steal_sock(skb)))
  310. return sk;
  311. else
  312. return __udp4_lib_lookup(dev_net(skb_dst(skb)->dev), iph->saddr, sport,
  313. iph->daddr, dport, inet_iif(skb),
  314. udptable);
  315. }
  316. struct sock *udp4_lib_lookup(struct net *net, __be32 saddr, __be16 sport,
  317. __be32 daddr, __be16 dport, int dif)
  318. {
  319. return __udp4_lib_lookup(net, saddr, sport, daddr, dport, dif, &udp_table);
  320. }
  321. EXPORT_SYMBOL_GPL(udp4_lib_lookup);
  322. static inline struct sock *udp_v4_mcast_next(struct net *net, struct sock *sk,
  323. __be16 loc_port, __be32 loc_addr,
  324. __be16 rmt_port, __be32 rmt_addr,
  325. int dif)
  326. {
  327. struct hlist_nulls_node *node;
  328. struct sock *s = sk;
  329. unsigned short hnum = ntohs(loc_port);
  330. sk_nulls_for_each_from(s, node) {
  331. struct inet_sock *inet = inet_sk(s);
  332. if (!net_eq(sock_net(s), net) ||
  333. s->sk_hash != hnum ||
  334. (inet->daddr && inet->daddr != rmt_addr) ||
  335. (inet->dport != rmt_port && inet->dport) ||
  336. (inet->rcv_saddr && inet->rcv_saddr != loc_addr) ||
  337. ipv6_only_sock(s) ||
  338. (s->sk_bound_dev_if && s->sk_bound_dev_if != dif))
  339. continue;
  340. if (!ip_mc_sf_allow(s, loc_addr, rmt_addr, dif))
  341. continue;
  342. goto found;
  343. }
  344. s = NULL;
  345. found:
  346. return s;
  347. }
  348. /*
  349. * This routine is called by the ICMP module when it gets some
  350. * sort of error condition. If err < 0 then the socket should
  351. * be closed and the error returned to the user. If err > 0
  352. * it's just the icmp type << 8 | icmp code.
  353. * Header points to the ip header of the error packet. We move
  354. * on past this. Then (as it used to claim before adjustment)
  355. * header points to the first 8 bytes of the udp header. We need
  356. * to find the appropriate port.
  357. */
  358. void __udp4_lib_err(struct sk_buff *skb, u32 info, struct udp_table *udptable)
  359. {
  360. struct inet_sock *inet;
  361. struct iphdr *iph = (struct iphdr *)skb->data;
  362. struct udphdr *uh = (struct udphdr *)(skb->data+(iph->ihl<<2));
  363. const int type = icmp_hdr(skb)->type;
  364. const int code = icmp_hdr(skb)->code;
  365. struct sock *sk;
  366. int harderr;
  367. int err;
  368. struct net *net = dev_net(skb->dev);
  369. sk = __udp4_lib_lookup(net, iph->daddr, uh->dest,
  370. iph->saddr, uh->source, skb->dev->ifindex, udptable);
  371. if (sk == NULL) {
  372. ICMP_INC_STATS_BH(net, ICMP_MIB_INERRORS);
  373. return; /* No socket for error */
  374. }
  375. err = 0;
  376. harderr = 0;
  377. inet = inet_sk(sk);
  378. switch (type) {
  379. default:
  380. case ICMP_TIME_EXCEEDED:
  381. err = EHOSTUNREACH;
  382. break;
  383. case ICMP_SOURCE_QUENCH:
  384. goto out;
  385. case ICMP_PARAMETERPROB:
  386. err = EPROTO;
  387. harderr = 1;
  388. break;
  389. case ICMP_DEST_UNREACH:
  390. if (code == ICMP_FRAG_NEEDED) { /* Path MTU discovery */
  391. if (inet->pmtudisc != IP_PMTUDISC_DONT) {
  392. err = EMSGSIZE;
  393. harderr = 1;
  394. break;
  395. }
  396. goto out;
  397. }
  398. err = EHOSTUNREACH;
  399. if (code <= NR_ICMP_UNREACH) {
  400. harderr = icmp_err_convert[code].fatal;
  401. err = icmp_err_convert[code].errno;
  402. }
  403. break;
  404. }
  405. /*
  406. * RFC1122: OK. Passes ICMP errors back to application, as per
  407. * 4.1.3.3.
  408. */
  409. if (!inet->recverr) {
  410. if (!harderr || sk->sk_state != TCP_ESTABLISHED)
  411. goto out;
  412. } else {
  413. ip_icmp_error(sk, skb, err, uh->dest, info, (u8 *)(uh+1));
  414. }
  415. sk->sk_err = err;
  416. sk->sk_error_report(sk);
  417. out:
  418. sock_put(sk);
  419. }
  420. void udp_err(struct sk_buff *skb, u32 info)
  421. {
  422. __udp4_lib_err(skb, info, &udp_table);
  423. }
  424. /*
  425. * Throw away all pending data and cancel the corking. Socket is locked.
  426. */
  427. void udp_flush_pending_frames(struct sock *sk)
  428. {
  429. struct udp_sock *up = udp_sk(sk);
  430. if (up->pending) {
  431. up->len = 0;
  432. up->pending = 0;
  433. ip_flush_pending_frames(sk);
  434. }
  435. }
  436. EXPORT_SYMBOL(udp_flush_pending_frames);
  437. /**
  438. * udp4_hwcsum_outgoing - handle outgoing HW checksumming
  439. * @sk: socket we are sending on
  440. * @skb: sk_buff containing the filled-in UDP header
  441. * (checksum field must be zeroed out)
  442. */
  443. static void udp4_hwcsum_outgoing(struct sock *sk, struct sk_buff *skb,
  444. __be32 src, __be32 dst, int len)
  445. {
  446. unsigned int offset;
  447. struct udphdr *uh = udp_hdr(skb);
  448. __wsum csum = 0;
  449. if (skb_queue_len(&sk->sk_write_queue) == 1) {
  450. /*
  451. * Only one fragment on the socket.
  452. */
  453. skb->csum_start = skb_transport_header(skb) - skb->head;
  454. skb->csum_offset = offsetof(struct udphdr, check);
  455. uh->check = ~csum_tcpudp_magic(src, dst, len, IPPROTO_UDP, 0);
  456. } else {
  457. /*
  458. * HW-checksum won't work as there are two or more
  459. * fragments on the socket so that all csums of sk_buffs
  460. * should be together
  461. */
  462. offset = skb_transport_offset(skb);
  463. skb->csum = skb_checksum(skb, offset, skb->len - offset, 0);
  464. skb->ip_summed = CHECKSUM_NONE;
  465. skb_queue_walk(&sk->sk_write_queue, skb) {
  466. csum = csum_add(csum, skb->csum);
  467. }
  468. uh->check = csum_tcpudp_magic(src, dst, len, IPPROTO_UDP, csum);
  469. if (uh->check == 0)
  470. uh->check = CSUM_MANGLED_0;
  471. }
  472. }
  473. /*
  474. * Push out all pending data as one UDP datagram. Socket is locked.
  475. */
  476. static int udp_push_pending_frames(struct sock *sk)
  477. {
  478. struct udp_sock *up = udp_sk(sk);
  479. struct inet_sock *inet = inet_sk(sk);
  480. struct flowi *fl = &inet->cork.fl;
  481. struct sk_buff *skb;
  482. struct udphdr *uh;
  483. int err = 0;
  484. int is_udplite = IS_UDPLITE(sk);
  485. __wsum csum = 0;
  486. /* Grab the skbuff where UDP header space exists. */
  487. if ((skb = skb_peek(&sk->sk_write_queue)) == NULL)
  488. goto out;
  489. /*
  490. * Create a UDP header
  491. */
  492. uh = udp_hdr(skb);
  493. uh->source = fl->fl_ip_sport;
  494. uh->dest = fl->fl_ip_dport;
  495. uh->len = htons(up->len);
  496. uh->check = 0;
  497. if (is_udplite) /* UDP-Lite */
  498. csum = udplite_csum_outgoing(sk, skb);
  499. else if (sk->sk_no_check == UDP_CSUM_NOXMIT) { /* UDP csum disabled */
  500. skb->ip_summed = CHECKSUM_NONE;
  501. goto send;
  502. } else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */
  503. udp4_hwcsum_outgoing(sk, skb, fl->fl4_src, fl->fl4_dst, up->len);
  504. goto send;
  505. } else /* `normal' UDP */
  506. csum = udp_csum_outgoing(sk, skb);
  507. /* add protocol-dependent pseudo-header */
  508. uh->check = csum_tcpudp_magic(fl->fl4_src, fl->fl4_dst, up->len,
  509. sk->sk_protocol, csum);
  510. if (uh->check == 0)
  511. uh->check = CSUM_MANGLED_0;
  512. send:
  513. err = ip_push_pending_frames(sk);
  514. if (err) {
  515. if (err == -ENOBUFS && !inet->recverr) {
  516. UDP_INC_STATS_USER(sock_net(sk),
  517. UDP_MIB_SNDBUFERRORS, is_udplite);
  518. err = 0;
  519. }
  520. } else
  521. UDP_INC_STATS_USER(sock_net(sk),
  522. UDP_MIB_OUTDATAGRAMS, is_udplite);
  523. out:
  524. up->len = 0;
  525. up->pending = 0;
  526. return err;
  527. }
  528. int udp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
  529. size_t len)
  530. {
  531. struct inet_sock *inet = inet_sk(sk);
  532. struct udp_sock *up = udp_sk(sk);
  533. int ulen = len;
  534. struct ipcm_cookie ipc;
  535. struct rtable *rt = NULL;
  536. int free = 0;
  537. int connected = 0;
  538. __be32 daddr, faddr, saddr;
  539. __be16 dport;
  540. u8 tos;
  541. int err, is_udplite = IS_UDPLITE(sk);
  542. int corkreq = up->corkflag || msg->msg_flags&MSG_MORE;
  543. int (*getfrag)(void *, char *, int, int, int, struct sk_buff *);
  544. if (len > 0xFFFF)
  545. return -EMSGSIZE;
  546. /*
  547. * Check the flags.
  548. */
  549. if (msg->msg_flags & MSG_OOB) /* Mirror BSD error message compatibility */
  550. return -EOPNOTSUPP;
  551. ipc.opt = NULL;
  552. ipc.shtx.flags = 0;
  553. if (up->pending) {
  554. /*
  555. * There are pending frames.
  556. * The socket lock must be held while it's corked.
  557. */
  558. lock_sock(sk);
  559. if (likely(up->pending)) {
  560. if (unlikely(up->pending != AF_INET)) {
  561. release_sock(sk);
  562. return -EINVAL;
  563. }
  564. goto do_append_data;
  565. }
  566. release_sock(sk);
  567. }
  568. ulen += sizeof(struct udphdr);
  569. /*
  570. * Get and verify the address.
  571. */
  572. if (msg->msg_name) {
  573. struct sockaddr_in * usin = (struct sockaddr_in *)msg->msg_name;
  574. if (msg->msg_namelen < sizeof(*usin))
  575. return -EINVAL;
  576. if (usin->sin_family != AF_INET) {
  577. if (usin->sin_family != AF_UNSPEC)
  578. return -EAFNOSUPPORT;
  579. }
  580. daddr = usin->sin_addr.s_addr;
  581. dport = usin->sin_port;
  582. if (dport == 0)
  583. return -EINVAL;
  584. } else {
  585. if (sk->sk_state != TCP_ESTABLISHED)
  586. return -EDESTADDRREQ;
  587. daddr = inet->daddr;
  588. dport = inet->dport;
  589. /* Open fast path for connected socket.
  590. Route will not be used, if at least one option is set.
  591. */
  592. connected = 1;
  593. }
  594. ipc.addr = inet->saddr;
  595. ipc.oif = sk->sk_bound_dev_if;
  596. err = sock_tx_timestamp(msg, sk, &ipc.shtx);
  597. if (err)
  598. return err;
  599. if (msg->msg_controllen) {
  600. err = ip_cmsg_send(sock_net(sk), msg, &ipc);
  601. if (err)
  602. return err;
  603. if (ipc.opt)
  604. free = 1;
  605. connected = 0;
  606. }
  607. if (!ipc.opt)
  608. ipc.opt = inet->opt;
  609. saddr = ipc.addr;
  610. ipc.addr = faddr = daddr;
  611. if (ipc.opt && ipc.opt->srr) {
  612. if (!daddr)
  613. return -EINVAL;
  614. faddr = ipc.opt->faddr;
  615. connected = 0;
  616. }
  617. tos = RT_TOS(inet->tos);
  618. if (sock_flag(sk, SOCK_LOCALROUTE) ||
  619. (msg->msg_flags & MSG_DONTROUTE) ||
  620. (ipc.opt && ipc.opt->is_strictroute)) {
  621. tos |= RTO_ONLINK;
  622. connected = 0;
  623. }
  624. if (ipv4_is_multicast(daddr)) {
  625. if (!ipc.oif)
  626. ipc.oif = inet->mc_index;
  627. if (!saddr)
  628. saddr = inet->mc_addr;
  629. connected = 0;
  630. }
  631. if (connected)
  632. rt = (struct rtable *)sk_dst_check(sk, 0);
  633. if (rt == NULL) {
  634. struct flowi fl = { .oif = ipc.oif,
  635. .mark = sk->sk_mark,
  636. .nl_u = { .ip4_u =
  637. { .daddr = faddr,
  638. .saddr = saddr,
  639. .tos = tos } },
  640. .proto = sk->sk_protocol,
  641. .flags = inet_sk_flowi_flags(sk),
  642. .uli_u = { .ports =
  643. { .sport = inet->sport,
  644. .dport = dport } } };
  645. struct net *net = sock_net(sk);
  646. security_sk_classify_flow(sk, &fl);
  647. err = ip_route_output_flow(net, &rt, &fl, sk, 1);
  648. if (err) {
  649. if (err == -ENETUNREACH)
  650. IP_INC_STATS_BH(net, IPSTATS_MIB_OUTNOROUTES);
  651. goto out;
  652. }
  653. err = -EACCES;
  654. if ((rt->rt_flags & RTCF_BROADCAST) &&
  655. !sock_flag(sk, SOCK_BROADCAST))
  656. goto out;
  657. if (connected)
  658. sk_dst_set(sk, dst_clone(&rt->u.dst));
  659. }
  660. if (msg->msg_flags&MSG_CONFIRM)
  661. goto do_confirm;
  662. back_from_confirm:
  663. saddr = rt->rt_src;
  664. if (!ipc.addr)
  665. daddr = ipc.addr = rt->rt_dst;
  666. lock_sock(sk);
  667. if (unlikely(up->pending)) {
  668. /* The socket is already corked while preparing it. */
  669. /* ... which is an evident application bug. --ANK */
  670. release_sock(sk);
  671. LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 2\n");
  672. err = -EINVAL;
  673. goto out;
  674. }
  675. /*
  676. * Now cork the socket to pend data.
  677. */
  678. inet->cork.fl.fl4_dst = daddr;
  679. inet->cork.fl.fl_ip_dport = dport;
  680. inet->cork.fl.fl4_src = saddr;
  681. inet->cork.fl.fl_ip_sport = inet->sport;
  682. up->pending = AF_INET;
  683. do_append_data:
  684. up->len += ulen;
  685. getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag;
  686. err = ip_append_data(sk, getfrag, msg->msg_iov, ulen,
  687. sizeof(struct udphdr), &ipc, &rt,
  688. corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags);
  689. if (err)
  690. udp_flush_pending_frames(sk);
  691. else if (!corkreq)
  692. err = udp_push_pending_frames(sk);
  693. else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
  694. up->pending = 0;
  695. release_sock(sk);
  696. out:
  697. ip_rt_put(rt);
  698. if (free)
  699. kfree(ipc.opt);
  700. if (!err)
  701. return len;
  702. /*
  703. * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting
  704. * ENOBUFS might not be good (it's not tunable per se), but otherwise
  705. * we don't have a good statistic (IpOutDiscards but it can be too many
  706. * things). We could add another new stat but at least for now that
  707. * seems like overkill.
  708. */
  709. if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
  710. UDP_INC_STATS_USER(sock_net(sk),
  711. UDP_MIB_SNDBUFERRORS, is_udplite);
  712. }
  713. return err;
  714. do_confirm:
  715. dst_confirm(&rt->u.dst);
  716. if (!(msg->msg_flags&MSG_PROBE) || len)
  717. goto back_from_confirm;
  718. err = 0;
  719. goto out;
  720. }
  721. EXPORT_SYMBOL(udp_sendmsg);
  722. int udp_sendpage(struct sock *sk, struct page *page, int offset,
  723. size_t size, int flags)
  724. {
  725. struct udp_sock *up = udp_sk(sk);
  726. int ret;
  727. if (!up->pending) {
  728. struct msghdr msg = { .msg_flags = flags|MSG_MORE };
  729. /* Call udp_sendmsg to specify destination address which
  730. * sendpage interface can't pass.
  731. * This will succeed only when the socket is connected.
  732. */
  733. ret = udp_sendmsg(NULL, sk, &msg, 0);
  734. if (ret < 0)
  735. return ret;
  736. }
  737. lock_sock(sk);
  738. if (unlikely(!up->pending)) {
  739. release_sock(sk);
  740. LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 3\n");
  741. return -EINVAL;
  742. }
  743. ret = ip_append_page(sk, page, offset, size, flags);
  744. if (ret == -EOPNOTSUPP) {
  745. release_sock(sk);
  746. return sock_no_sendpage(sk->sk_socket, page, offset,
  747. size, flags);
  748. }
  749. if (ret < 0) {
  750. udp_flush_pending_frames(sk);
  751. goto out;
  752. }
  753. up->len += size;
  754. if (!(up->corkflag || (flags&MSG_MORE)))
  755. ret = udp_push_pending_frames(sk);
  756. if (!ret)
  757. ret = size;
  758. out:
  759. release_sock(sk);
  760. return ret;
  761. }
  762. /*
  763. * IOCTL requests applicable to the UDP protocol
  764. */
  765. int udp_ioctl(struct sock *sk, int cmd, unsigned long arg)
  766. {
  767. switch (cmd) {
  768. case SIOCOUTQ:
  769. {
  770. int amount = sk_wmem_alloc_get(sk);
  771. return put_user(amount, (int __user *)arg);
  772. }
  773. case SIOCINQ:
  774. {
  775. struct sk_buff *skb;
  776. unsigned long amount;
  777. amount = 0;
  778. spin_lock_bh(&sk->sk_receive_queue.lock);
  779. skb = skb_peek(&sk->sk_receive_queue);
  780. if (skb != NULL) {
  781. /*
  782. * We will only return the amount
  783. * of this packet since that is all
  784. * that will be read.
  785. */
  786. amount = skb->len - sizeof(struct udphdr);
  787. }
  788. spin_unlock_bh(&sk->sk_receive_queue.lock);
  789. return put_user(amount, (int __user *)arg);
  790. }
  791. default:
  792. return -ENOIOCTLCMD;
  793. }
  794. return 0;
  795. }
  796. EXPORT_SYMBOL(udp_ioctl);
  797. /*
  798. * This should be easy, if there is something there we
  799. * return it, otherwise we block.
  800. */
  801. int udp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
  802. size_t len, int noblock, int flags, int *addr_len)
  803. {
  804. struct inet_sock *inet = inet_sk(sk);
  805. struct sockaddr_in *sin = (struct sockaddr_in *)msg->msg_name;
  806. struct sk_buff *skb;
  807. unsigned int ulen, copied;
  808. int peeked;
  809. int err;
  810. int is_udplite = IS_UDPLITE(sk);
  811. /*
  812. * Check any passed addresses
  813. */
  814. if (addr_len)
  815. *addr_len = sizeof(*sin);
  816. if (flags & MSG_ERRQUEUE)
  817. return ip_recv_error(sk, msg, len);
  818. try_again:
  819. skb = __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0),
  820. &peeked, &err);
  821. if (!skb)
  822. goto out;
  823. ulen = skb->len - sizeof(struct udphdr);
  824. copied = len;
  825. if (copied > ulen)
  826. copied = ulen;
  827. else if (copied < ulen)
  828. msg->msg_flags |= MSG_TRUNC;
  829. /*
  830. * If checksum is needed at all, try to do it while copying the
  831. * data. If the data is truncated, or if we only want a partial
  832. * coverage checksum (UDP-Lite), do it before the copy.
  833. */
  834. if (copied < ulen || UDP_SKB_CB(skb)->partial_cov) {
  835. if (udp_lib_checksum_complete(skb))
  836. goto csum_copy_err;
  837. }
  838. if (skb_csum_unnecessary(skb))
  839. err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr),
  840. msg->msg_iov, copied);
  841. else {
  842. err = skb_copy_and_csum_datagram_iovec(skb,
  843. sizeof(struct udphdr),
  844. msg->msg_iov);
  845. if (err == -EINVAL)
  846. goto csum_copy_err;
  847. }
  848. if (err)
  849. goto out_free;
  850. if (!peeked)
  851. UDP_INC_STATS_USER(sock_net(sk),
  852. UDP_MIB_INDATAGRAMS, is_udplite);
  853. sock_recv_timestamp(msg, sk, skb);
  854. /* Copy the address. */
  855. if (sin) {
  856. sin->sin_family = AF_INET;
  857. sin->sin_port = udp_hdr(skb)->source;
  858. sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
  859. memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
  860. }
  861. if (inet->cmsg_flags)
  862. ip_cmsg_recv(msg, skb);
  863. err = copied;
  864. if (flags & MSG_TRUNC)
  865. err = ulen;
  866. out_free:
  867. lock_sock(sk);
  868. skb_free_datagram(sk, skb);
  869. release_sock(sk);
  870. out:
  871. return err;
  872. csum_copy_err:
  873. lock_sock(sk);
  874. if (!skb_kill_datagram(sk, skb, flags))
  875. UDP_INC_STATS_USER(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
  876. release_sock(sk);
  877. if (noblock)
  878. return -EAGAIN;
  879. goto try_again;
  880. }
  881. int udp_disconnect(struct sock *sk, int flags)
  882. {
  883. struct inet_sock *inet = inet_sk(sk);
  884. /*
  885. * 1003.1g - break association.
  886. */
  887. sk->sk_state = TCP_CLOSE;
  888. inet->daddr = 0;
  889. inet->dport = 0;
  890. sk->sk_bound_dev_if = 0;
  891. if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK))
  892. inet_reset_saddr(sk);
  893. if (!(sk->sk_userlocks & SOCK_BINDPORT_LOCK)) {
  894. sk->sk_prot->unhash(sk);
  895. inet->sport = 0;
  896. }
  897. sk_dst_reset(sk);
  898. return 0;
  899. }
  900. EXPORT_SYMBOL(udp_disconnect);
  901. void udp_lib_unhash(struct sock *sk)
  902. {
  903. if (sk_hashed(sk)) {
  904. struct udp_table *udptable = sk->sk_prot->h.udp_table;
  905. struct udp_hslot *hslot = udp_hashslot(udptable, sock_net(sk),
  906. sk->sk_hash);
  907. spin_lock_bh(&hslot->lock);
  908. if (sk_nulls_del_node_init_rcu(sk)) {
  909. inet_sk(sk)->num = 0;
  910. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
  911. }
  912. spin_unlock_bh(&hslot->lock);
  913. }
  914. }
  915. EXPORT_SYMBOL(udp_lib_unhash);
  916. static int __udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
  917. {
  918. int is_udplite = IS_UDPLITE(sk);
  919. int rc;
  920. if ((rc = sock_queue_rcv_skb(sk, skb)) < 0) {
  921. /* Note that an ENOMEM error is charged twice */
  922. if (rc == -ENOMEM) {
  923. UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_RCVBUFERRORS,
  924. is_udplite);
  925. atomic_inc(&sk->sk_drops);
  926. }
  927. goto drop;
  928. }
  929. return 0;
  930. drop:
  931. UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
  932. kfree_skb(skb);
  933. return -1;
  934. }
  935. /* returns:
  936. * -1: error
  937. * 0: success
  938. * >0: "udp encap" protocol resubmission
  939. *
  940. * Note that in the success and error cases, the skb is assumed to
  941. * have either been requeued or freed.
  942. */
  943. int udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
  944. {
  945. struct udp_sock *up = udp_sk(sk);
  946. int rc;
  947. int is_udplite = IS_UDPLITE(sk);
  948. /*
  949. * Charge it to the socket, dropping if the queue is full.
  950. */
  951. if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb))
  952. goto drop;
  953. nf_reset(skb);
  954. if (up->encap_type) {
  955. /*
  956. * This is an encapsulation socket so pass the skb to
  957. * the socket's udp_encap_rcv() hook. Otherwise, just
  958. * fall through and pass this up the UDP socket.
  959. * up->encap_rcv() returns the following value:
  960. * =0 if skb was successfully passed to the encap
  961. * handler or was discarded by it.
  962. * >0 if skb should be passed on to UDP.
  963. * <0 if skb should be resubmitted as proto -N
  964. */
  965. /* if we're overly short, let UDP handle it */
  966. if (skb->len > sizeof(struct udphdr) &&
  967. up->encap_rcv != NULL) {
  968. int ret;
  969. ret = (*up->encap_rcv)(sk, skb);
  970. if (ret <= 0) {
  971. UDP_INC_STATS_BH(sock_net(sk),
  972. UDP_MIB_INDATAGRAMS,
  973. is_udplite);
  974. return -ret;
  975. }
  976. }
  977. /* FALLTHROUGH -- it's a UDP Packet */
  978. }
  979. /*
  980. * UDP-Lite specific tests, ignored on UDP sockets
  981. */
  982. if ((is_udplite & UDPLITE_RECV_CC) && UDP_SKB_CB(skb)->partial_cov) {
  983. /*
  984. * MIB statistics other than incrementing the error count are
  985. * disabled for the following two types of errors: these depend
  986. * on the application settings, not on the functioning of the
  987. * protocol stack as such.
  988. *
  989. * RFC 3828 here recommends (sec 3.3): "There should also be a
  990. * way ... to ... at least let the receiving application block
  991. * delivery of packets with coverage values less than a value
  992. * provided by the application."
  993. */
  994. if (up->pcrlen == 0) { /* full coverage was set */
  995. LIMIT_NETDEBUG(KERN_WARNING "UDPLITE: partial coverage "
  996. "%d while full coverage %d requested\n",
  997. UDP_SKB_CB(skb)->cscov, skb->len);
  998. goto drop;
  999. }
  1000. /* The next case involves violating the min. coverage requested
  1001. * by the receiver. This is subtle: if receiver wants x and x is
  1002. * greater than the buffersize/MTU then receiver will complain
  1003. * that it wants x while sender emits packets of smaller size y.
  1004. * Therefore the above ...()->partial_cov statement is essential.
  1005. */
  1006. if (UDP_SKB_CB(skb)->cscov < up->pcrlen) {
  1007. LIMIT_NETDEBUG(KERN_WARNING
  1008. "UDPLITE: coverage %d too small, need min %d\n",
  1009. UDP_SKB_CB(skb)->cscov, up->pcrlen);
  1010. goto drop;
  1011. }
  1012. }
  1013. if (sk->sk_filter) {
  1014. if (udp_lib_checksum_complete(skb))
  1015. goto drop;
  1016. }
  1017. rc = 0;
  1018. bh_lock_sock(sk);
  1019. if (!sock_owned_by_user(sk))
  1020. rc = __udp_queue_rcv_skb(sk, skb);
  1021. else
  1022. sk_add_backlog(sk, skb);
  1023. bh_unlock_sock(sk);
  1024. return rc;
  1025. drop:
  1026. UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
  1027. kfree_skb(skb);
  1028. return -1;
  1029. }
  1030. /*
  1031. * Multicasts and broadcasts go to each listener.
  1032. *
  1033. * Note: called only from the BH handler context,
  1034. * so we don't need to lock the hashes.
  1035. */
  1036. static int __udp4_lib_mcast_deliver(struct net *net, struct sk_buff *skb,
  1037. struct udphdr *uh,
  1038. __be32 saddr, __be32 daddr,
  1039. struct udp_table *udptable)
  1040. {
  1041. struct sock *sk;
  1042. struct udp_hslot *hslot = udp_hashslot(udptable, net, ntohs(uh->dest));
  1043. int dif;
  1044. spin_lock(&hslot->lock);
  1045. sk = sk_nulls_head(&hslot->head);
  1046. dif = skb->dev->ifindex;
  1047. sk = udp_v4_mcast_next(net, sk, uh->dest, daddr, uh->source, saddr, dif);
  1048. if (sk) {
  1049. struct sock *sknext = NULL;
  1050. do {
  1051. struct sk_buff *skb1 = skb;
  1052. sknext = udp_v4_mcast_next(net, sk_nulls_next(sk), uh->dest,
  1053. daddr, uh->source, saddr,
  1054. dif);
  1055. if (sknext)
  1056. skb1 = skb_clone(skb, GFP_ATOMIC);
  1057. if (skb1) {
  1058. int ret = udp_queue_rcv_skb(sk, skb1);
  1059. if (ret > 0)
  1060. /* we should probably re-process instead
  1061. * of dropping packets here. */
  1062. kfree_skb(skb1);
  1063. }
  1064. sk = sknext;
  1065. } while (sknext);
  1066. } else
  1067. consume_skb(skb);
  1068. spin_unlock(&hslot->lock);
  1069. return 0;
  1070. }
  1071. /* Initialize UDP checksum. If exited with zero value (success),
  1072. * CHECKSUM_UNNECESSARY means, that no more checks are required.
  1073. * Otherwise, csum completion requires chacksumming packet body,
  1074. * including udp header and folding it to skb->csum.
  1075. */
  1076. static inline int udp4_csum_init(struct sk_buff *skb, struct udphdr *uh,
  1077. int proto)
  1078. {
  1079. const struct iphdr *iph;
  1080. int err;
  1081. UDP_SKB_CB(skb)->partial_cov = 0;
  1082. UDP_SKB_CB(skb)->cscov = skb->len;
  1083. if (proto == IPPROTO_UDPLITE) {
  1084. err = udplite_checksum_init(skb, uh);
  1085. if (err)
  1086. return err;
  1087. }
  1088. iph = ip_hdr(skb);
  1089. if (uh->check == 0) {
  1090. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1091. } else if (skb->ip_summed == CHECKSUM_COMPLETE) {
  1092. if (!csum_tcpudp_magic(iph->saddr, iph->daddr, skb->len,
  1093. proto, skb->csum))
  1094. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1095. }
  1096. if (!skb_csum_unnecessary(skb))
  1097. skb->csum = csum_tcpudp_nofold(iph->saddr, iph->daddr,
  1098. skb->len, proto, 0);
  1099. /* Probably, we should checksum udp header (it should be in cache
  1100. * in any case) and data in tiny packets (< rx copybreak).
  1101. */
  1102. return 0;
  1103. }
  1104. /*
  1105. * All we need to do is get the socket, and then do a checksum.
  1106. */
  1107. int __udp4_lib_rcv(struct sk_buff *skb, struct udp_table *udptable,
  1108. int proto)
  1109. {
  1110. struct sock *sk;
  1111. struct udphdr *uh;
  1112. unsigned short ulen;
  1113. struct rtable *rt = skb_rtable(skb);
  1114. __be32 saddr, daddr;
  1115. struct net *net = dev_net(skb->dev);
  1116. /*
  1117. * Validate the packet.
  1118. */
  1119. if (!pskb_may_pull(skb, sizeof(struct udphdr)))
  1120. goto drop; /* No space for header. */
  1121. uh = udp_hdr(skb);
  1122. ulen = ntohs(uh->len);
  1123. if (ulen > skb->len)
  1124. goto short_packet;
  1125. if (proto == IPPROTO_UDP) {
  1126. /* UDP validates ulen. */
  1127. if (ulen < sizeof(*uh) || pskb_trim_rcsum(skb, ulen))
  1128. goto short_packet;
  1129. uh = udp_hdr(skb);
  1130. }
  1131. if (udp4_csum_init(skb, uh, proto))
  1132. goto csum_error;
  1133. saddr = ip_hdr(skb)->saddr;
  1134. daddr = ip_hdr(skb)->daddr;
  1135. if (rt->rt_flags & (RTCF_BROADCAST|RTCF_MULTICAST))
  1136. return __udp4_lib_mcast_deliver(net, skb, uh,
  1137. saddr, daddr, udptable);
  1138. sk = __udp4_lib_lookup_skb(skb, uh->source, uh->dest, udptable);
  1139. if (sk != NULL) {
  1140. int ret = udp_queue_rcv_skb(sk, skb);
  1141. sock_put(sk);
  1142. /* a return value > 0 means to resubmit the input, but
  1143. * it wants the return to be -protocol, or 0
  1144. */
  1145. if (ret > 0)
  1146. return -ret;
  1147. return 0;
  1148. }
  1149. if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
  1150. goto drop;
  1151. nf_reset(skb);
  1152. /* No socket. Drop packet silently, if checksum is wrong */
  1153. if (udp_lib_checksum_complete(skb))
  1154. goto csum_error;
  1155. UDP_INC_STATS_BH(net, UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE);
  1156. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
  1157. /*
  1158. * Hmm. We got an UDP packet to a port to which we
  1159. * don't wanna listen. Ignore it.
  1160. */
  1161. kfree_skb(skb);
  1162. return 0;
  1163. short_packet:
  1164. LIMIT_NETDEBUG(KERN_DEBUG "UDP%s: short packet: From %pI4:%u %d/%d to %pI4:%u\n",
  1165. proto == IPPROTO_UDPLITE ? "-Lite" : "",
  1166. &saddr,
  1167. ntohs(uh->source),
  1168. ulen,
  1169. skb->len,
  1170. &daddr,
  1171. ntohs(uh->dest));
  1172. goto drop;
  1173. csum_error:
  1174. /*
  1175. * RFC1122: OK. Discards the bad packet silently (as far as
  1176. * the network is concerned, anyway) as per 4.1.3.4 (MUST).
  1177. */
  1178. LIMIT_NETDEBUG(KERN_DEBUG "UDP%s: bad checksum. From %pI4:%u to %pI4:%u ulen %d\n",
  1179. proto == IPPROTO_UDPLITE ? "-Lite" : "",
  1180. &saddr,
  1181. ntohs(uh->source),
  1182. &daddr,
  1183. ntohs(uh->dest),
  1184. ulen);
  1185. drop:
  1186. UDP_INC_STATS_BH(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE);
  1187. kfree_skb(skb);
  1188. return 0;
  1189. }
  1190. int udp_rcv(struct sk_buff *skb)
  1191. {
  1192. return __udp4_lib_rcv(skb, &udp_table, IPPROTO_UDP);
  1193. }
  1194. void udp_destroy_sock(struct sock *sk)
  1195. {
  1196. lock_sock(sk);
  1197. udp_flush_pending_frames(sk);
  1198. release_sock(sk);
  1199. }
  1200. /*
  1201. * Socket option code for UDP
  1202. */
  1203. int udp_lib_setsockopt(struct sock *sk, int level, int optname,
  1204. char __user *optval, unsigned int optlen,
  1205. int (*push_pending_frames)(struct sock *))
  1206. {
  1207. struct udp_sock *up = udp_sk(sk);
  1208. int val;
  1209. int err = 0;
  1210. int is_udplite = IS_UDPLITE(sk);
  1211. if (optlen < sizeof(int))
  1212. return -EINVAL;
  1213. if (get_user(val, (int __user *)optval))
  1214. return -EFAULT;
  1215. switch (optname) {
  1216. case UDP_CORK:
  1217. if (val != 0) {
  1218. up->corkflag = 1;
  1219. } else {
  1220. up->corkflag = 0;
  1221. lock_sock(sk);
  1222. (*push_pending_frames)(sk);
  1223. release_sock(sk);
  1224. }
  1225. break;
  1226. case UDP_ENCAP:
  1227. switch (val) {
  1228. case 0:
  1229. case UDP_ENCAP_ESPINUDP:
  1230. case UDP_ENCAP_ESPINUDP_NON_IKE:
  1231. up->encap_rcv = xfrm4_udp_encap_rcv;
  1232. /* FALLTHROUGH */
  1233. case UDP_ENCAP_L2TPINUDP:
  1234. up->encap_type = val;
  1235. break;
  1236. default:
  1237. err = -ENOPROTOOPT;
  1238. break;
  1239. }
  1240. break;
  1241. /*
  1242. * UDP-Lite's partial checksum coverage (RFC 3828).
  1243. */
  1244. /* The sender sets actual checksum coverage length via this option.
  1245. * The case coverage > packet length is handled by send module. */
  1246. case UDPLITE_SEND_CSCOV:
  1247. if (!is_udplite) /* Disable the option on UDP sockets */
  1248. return -ENOPROTOOPT;
  1249. if (val != 0 && val < 8) /* Illegal coverage: use default (8) */
  1250. val = 8;
  1251. else if (val > USHORT_MAX)
  1252. val = USHORT_MAX;
  1253. up->pcslen = val;
  1254. up->pcflag |= UDPLITE_SEND_CC;
  1255. break;
  1256. /* The receiver specifies a minimum checksum coverage value. To make
  1257. * sense, this should be set to at least 8 (as done below). If zero is
  1258. * used, this again means full checksum coverage. */
  1259. case UDPLITE_RECV_CSCOV:
  1260. if (!is_udplite) /* Disable the option on UDP sockets */
  1261. return -ENOPROTOOPT;
  1262. if (val != 0 && val < 8) /* Avoid silly minimal values. */
  1263. val = 8;
  1264. else if (val > USHORT_MAX)
  1265. val = USHORT_MAX;
  1266. up->pcrlen = val;
  1267. up->pcflag |= UDPLITE_RECV_CC;
  1268. break;
  1269. default:
  1270. err = -ENOPROTOOPT;
  1271. break;
  1272. }
  1273. return err;
  1274. }
  1275. EXPORT_SYMBOL(udp_lib_setsockopt);
  1276. int udp_setsockopt(struct sock *sk, int level, int optname,
  1277. char __user *optval, unsigned int optlen)
  1278. {
  1279. if (level == SOL_UDP || level == SOL_UDPLITE)
  1280. return udp_lib_setsockopt(sk, level, optname, optval, optlen,
  1281. udp_push_pending_frames);
  1282. return ip_setsockopt(sk, level, optname, optval, optlen);
  1283. }
  1284. #ifdef CONFIG_COMPAT
  1285. int compat_udp_setsockopt(struct sock *sk, int level, int optname,
  1286. char __user *optval, unsigned int optlen)
  1287. {
  1288. if (level == SOL_UDP || level == SOL_UDPLITE)
  1289. return udp_lib_setsockopt(sk, level, optname, optval, optlen,
  1290. udp_push_pending_frames);
  1291. return compat_ip_setsockopt(sk, level, optname, optval, optlen);
  1292. }
  1293. #endif
  1294. int udp_lib_getsockopt(struct sock *sk, int level, int optname,
  1295. char __user *optval, int __user *optlen)
  1296. {
  1297. struct udp_sock *up = udp_sk(sk);
  1298. int val, len;
  1299. if (get_user(len, optlen))
  1300. return -EFAULT;
  1301. len = min_t(unsigned int, len, sizeof(int));
  1302. if (len < 0)
  1303. return -EINVAL;
  1304. switch (optname) {
  1305. case UDP_CORK:
  1306. val = up->corkflag;
  1307. break;
  1308. case UDP_ENCAP:
  1309. val = up->encap_type;
  1310. break;
  1311. /* The following two cannot be changed on UDP sockets, the return is
  1312. * always 0 (which corresponds to the full checksum coverage of UDP). */
  1313. case UDPLITE_SEND_CSCOV:
  1314. val = up->pcslen;
  1315. break;
  1316. case UDPLITE_RECV_CSCOV:
  1317. val = up->pcrlen;
  1318. break;
  1319. default:
  1320. return -ENOPROTOOPT;
  1321. }
  1322. if (put_user(len, optlen))
  1323. return -EFAULT;
  1324. if (copy_to_user(optval, &val, len))
  1325. return -EFAULT;
  1326. return 0;
  1327. }
  1328. EXPORT_SYMBOL(udp_lib_getsockopt);
  1329. int udp_getsockopt(struct sock *sk, int level, int optname,
  1330. char __user *optval, int __user *optlen)
  1331. {
  1332. if (level == SOL_UDP || level == SOL_UDPLITE)
  1333. return udp_lib_getsockopt(sk, level, optname, optval, optlen);
  1334. return ip_getsockopt(sk, level, optname, optval, optlen);
  1335. }
  1336. #ifdef CONFIG_COMPAT
  1337. int compat_udp_getsockopt(struct sock *sk, int level, int optname,
  1338. char __user *optval, int __user *optlen)
  1339. {
  1340. if (level == SOL_UDP || level == SOL_UDPLITE)
  1341. return udp_lib_getsockopt(sk, level, optname, optval, optlen);
  1342. return compat_ip_getsockopt(sk, level, optname, optval, optlen);
  1343. }
  1344. #endif
  1345. /**
  1346. * udp_poll - wait for a UDP event.
  1347. * @file - file struct
  1348. * @sock - socket
  1349. * @wait - poll table
  1350. *
  1351. * This is same as datagram poll, except for the special case of
  1352. * blocking sockets. If application is using a blocking fd
  1353. * and a packet with checksum error is in the queue;
  1354. * then it could get return from select indicating data available
  1355. * but then block when reading it. Add special case code
  1356. * to work around these arguably broken applications.
  1357. */
  1358. unsigned int udp_poll(struct file *file, struct socket *sock, poll_table *wait)
  1359. {
  1360. unsigned int mask = datagram_poll(file, sock, wait);
  1361. struct sock *sk = sock->sk;
  1362. int is_lite = IS_UDPLITE(sk);
  1363. /* Check for false positives due to checksum errors */
  1364. if ((mask & POLLRDNORM) &&
  1365. !(file->f_flags & O_NONBLOCK) &&
  1366. !(sk->sk_shutdown & RCV_SHUTDOWN)) {
  1367. struct sk_buff_head *rcvq = &sk->sk_receive_queue;
  1368. struct sk_buff *skb;
  1369. spin_lock_bh(&rcvq->lock);
  1370. while ((skb = skb_peek(rcvq)) != NULL &&
  1371. udp_lib_checksum_complete(skb)) {
  1372. UDP_INC_STATS_BH(sock_net(sk),
  1373. UDP_MIB_INERRORS, is_lite);
  1374. __skb_unlink(skb, rcvq);
  1375. kfree_skb(skb);
  1376. }
  1377. spin_unlock_bh(&rcvq->lock);
  1378. /* nothing to see, move along */
  1379. if (skb == NULL)
  1380. mask &= ~(POLLIN | POLLRDNORM);
  1381. }
  1382. return mask;
  1383. }
  1384. EXPORT_SYMBOL(udp_poll);
  1385. struct proto udp_prot = {
  1386. .name = "UDP",
  1387. .owner = THIS_MODULE,
  1388. .close = udp_lib_close,
  1389. .connect = ip4_datagram_connect,
  1390. .disconnect = udp_disconnect,
  1391. .ioctl = udp_ioctl,
  1392. .destroy = udp_destroy_sock,
  1393. .setsockopt = udp_setsockopt,
  1394. .getsockopt = udp_getsockopt,
  1395. .sendmsg = udp_sendmsg,
  1396. .recvmsg = udp_recvmsg,
  1397. .sendpage = udp_sendpage,
  1398. .backlog_rcv = __udp_queue_rcv_skb,
  1399. .hash = udp_lib_hash,
  1400. .unhash = udp_lib_unhash,
  1401. .get_port = udp_v4_get_port,
  1402. .memory_allocated = &udp_memory_allocated,
  1403. .sysctl_mem = sysctl_udp_mem,
  1404. .sysctl_wmem = &sysctl_udp_wmem_min,
  1405. .sysctl_rmem = &sysctl_udp_rmem_min,
  1406. .obj_size = sizeof(struct udp_sock),
  1407. .slab_flags = SLAB_DESTROY_BY_RCU,
  1408. .h.udp_table = &udp_table,
  1409. #ifdef CONFIG_COMPAT
  1410. .compat_setsockopt = compat_udp_setsockopt,
  1411. .compat_getsockopt = compat_udp_getsockopt,
  1412. #endif
  1413. };
  1414. EXPORT_SYMBOL(udp_prot);
  1415. /* ------------------------------------------------------------------------ */
  1416. #ifdef CONFIG_PROC_FS
  1417. static struct sock *udp_get_first(struct seq_file *seq, int start)
  1418. {
  1419. struct sock *sk;
  1420. struct udp_iter_state *state = seq->private;
  1421. struct net *net = seq_file_net(seq);
  1422. for (state->bucket = start; state->bucket <= state->udp_table->mask;
  1423. ++state->bucket) {
  1424. struct hlist_nulls_node *node;
  1425. struct udp_hslot *hslot = &state->udp_table->hash[state->bucket];
  1426. if (hlist_nulls_empty(&hslot->head))
  1427. continue;
  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 <= state->udp_table->mask)
  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 = MAX_UDP_PORTS;
  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 <= state->udp_table->mask)
  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, "%5d: %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. static __initdata unsigned long uhash_entries;
  1590. static int __init set_uhash_entries(char *str)
  1591. {
  1592. if (!str)
  1593. return 0;
  1594. uhash_entries = simple_strtoul(str, &str, 0);
  1595. if (uhash_entries && uhash_entries < UDP_HTABLE_SIZE_MIN)
  1596. uhash_entries = UDP_HTABLE_SIZE_MIN;
  1597. return 1;
  1598. }
  1599. __setup("uhash_entries=", set_uhash_entries);
  1600. void __init udp_table_init(struct udp_table *table, const char *name)
  1601. {
  1602. unsigned int i;
  1603. if (!CONFIG_BASE_SMALL)
  1604. table->hash = alloc_large_system_hash(name,
  1605. sizeof(struct udp_hslot),
  1606. uhash_entries,
  1607. 21, /* one slot per 2 MB */
  1608. 0,
  1609. &table->log,
  1610. &table->mask,
  1611. 64 * 1024);
  1612. /*
  1613. * Make sure hash table has the minimum size
  1614. */
  1615. if (CONFIG_BASE_SMALL || table->mask < UDP_HTABLE_SIZE_MIN - 1) {
  1616. table->hash = kmalloc(UDP_HTABLE_SIZE_MIN *
  1617. sizeof(struct udp_hslot), GFP_KERNEL);
  1618. if (!table->hash)
  1619. panic(name);
  1620. table->log = ilog2(UDP_HTABLE_SIZE_MIN);
  1621. table->mask = UDP_HTABLE_SIZE_MIN - 1;
  1622. }
  1623. for (i = 0; i <= table->mask; i++) {
  1624. INIT_HLIST_NULLS_HEAD(&table->hash[i].head, i);
  1625. spin_lock_init(&table->hash[i].lock);
  1626. }
  1627. }
  1628. void __init udp_init(void)
  1629. {
  1630. unsigned long nr_pages, limit;
  1631. udp_table_init(&udp_table, "UDP");
  1632. /* Set the pressure threshold up by the same strategy of TCP. It is a
  1633. * fraction of global memory that is up to 1/2 at 256 MB, decreasing
  1634. * toward zero with the amount of memory, with a floor of 128 pages.
  1635. */
  1636. nr_pages = totalram_pages - totalhigh_pages;
  1637. limit = min(nr_pages, 1UL<<(28-PAGE_SHIFT)) >> (20-PAGE_SHIFT);
  1638. limit = (limit * (nr_pages >> (20-PAGE_SHIFT))) >> (PAGE_SHIFT-11);
  1639. limit = max(limit, 128UL);
  1640. sysctl_udp_mem[0] = limit / 4 * 3;
  1641. sysctl_udp_mem[1] = limit;
  1642. sysctl_udp_mem[2] = sysctl_udp_mem[0] * 2;
  1643. sysctl_udp_rmem_min = SK_MEM_QUANTUM;
  1644. sysctl_udp_wmem_min = SK_MEM_QUANTUM;
  1645. }
  1646. int udp4_ufo_send_check(struct sk_buff *skb)
  1647. {
  1648. const struct iphdr *iph;
  1649. struct udphdr *uh;
  1650. if (!pskb_may_pull(skb, sizeof(*uh)))
  1651. return -EINVAL;
  1652. iph = ip_hdr(skb);
  1653. uh = udp_hdr(skb);
  1654. uh->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr, skb->len,
  1655. IPPROTO_UDP, 0);
  1656. skb->csum_start = skb_transport_header(skb) - skb->head;
  1657. skb->csum_offset = offsetof(struct udphdr, check);
  1658. skb->ip_summed = CHECKSUM_PARTIAL;
  1659. return 0;
  1660. }
  1661. struct sk_buff *udp4_ufo_fragment(struct sk_buff *skb, int features)
  1662. {
  1663. struct sk_buff *segs = ERR_PTR(-EINVAL);
  1664. unsigned int mss;
  1665. int offset;
  1666. __wsum csum;
  1667. mss = skb_shinfo(skb)->gso_size;
  1668. if (unlikely(skb->len <= mss))
  1669. goto out;
  1670. if (skb_gso_ok(skb, features | NETIF_F_GSO_ROBUST)) {
  1671. /* Packet is from an untrusted source, reset gso_segs. */
  1672. int type = skb_shinfo(skb)->gso_type;
  1673. if (unlikely(type & ~(SKB_GSO_UDP | SKB_GSO_DODGY) ||
  1674. !(type & (SKB_GSO_UDP))))
  1675. goto out;
  1676. skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(skb->len, mss);
  1677. segs = NULL;
  1678. goto out;
  1679. }
  1680. /* Do software UFO. Complete and fill in the UDP checksum as HW cannot
  1681. * do checksum of UDP packets sent as multiple IP fragments.
  1682. */
  1683. offset = skb->csum_start - skb_headroom(skb);
  1684. csum = skb_checksum(skb, offset, skb->len - offset, 0);
  1685. offset += skb->csum_offset;
  1686. *(__sum16 *)(skb->data + offset) = csum_fold(csum);
  1687. skb->ip_summed = CHECKSUM_NONE;
  1688. /* Fragment the skb. IP headers of the fragments are updated in
  1689. * inet_gso_segment()
  1690. */
  1691. segs = skb_segment(skb, features);
  1692. out:
  1693. return segs;
  1694. }