udp.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949
  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. * first_packet_length - return length of first packet in receive queue
  764. * @sk: socket
  765. *
  766. * Drops all bad checksum frames, until a valid one is found.
  767. * Returns the length of found skb, or 0 if none is found.
  768. */
  769. static unsigned int first_packet_length(struct sock *sk)
  770. {
  771. struct sk_buff_head list_kill, *rcvq = &sk->sk_receive_queue;
  772. struct sk_buff *skb;
  773. unsigned int res;
  774. __skb_queue_head_init(&list_kill);
  775. spin_lock_bh(&rcvq->lock);
  776. while ((skb = skb_peek(rcvq)) != NULL &&
  777. udp_lib_checksum_complete(skb)) {
  778. UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS,
  779. IS_UDPLITE(sk));
  780. __skb_unlink(skb, rcvq);
  781. __skb_queue_tail(&list_kill, skb);
  782. }
  783. res = skb ? skb->len : 0;
  784. spin_unlock_bh(&rcvq->lock);
  785. if (!skb_queue_empty(&list_kill)) {
  786. lock_sock(sk);
  787. __skb_queue_purge(&list_kill);
  788. sk_mem_reclaim_partial(sk);
  789. release_sock(sk);
  790. }
  791. return res;
  792. }
  793. /*
  794. * IOCTL requests applicable to the UDP protocol
  795. */
  796. int udp_ioctl(struct sock *sk, int cmd, unsigned long arg)
  797. {
  798. switch (cmd) {
  799. case SIOCOUTQ:
  800. {
  801. int amount = sk_wmem_alloc_get(sk);
  802. return put_user(amount, (int __user *)arg);
  803. }
  804. case SIOCINQ:
  805. {
  806. unsigned int amount = first_packet_length(sk);
  807. if (amount)
  808. /*
  809. * We will only return the amount
  810. * of this packet since that is all
  811. * that will be read.
  812. */
  813. amount -= sizeof(struct udphdr);
  814. return put_user(amount, (int __user *)arg);
  815. }
  816. default:
  817. return -ENOIOCTLCMD;
  818. }
  819. return 0;
  820. }
  821. EXPORT_SYMBOL(udp_ioctl);
  822. /*
  823. * This should be easy, if there is something there we
  824. * return it, otherwise we block.
  825. */
  826. int udp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
  827. size_t len, int noblock, int flags, int *addr_len)
  828. {
  829. struct inet_sock *inet = inet_sk(sk);
  830. struct sockaddr_in *sin = (struct sockaddr_in *)msg->msg_name;
  831. struct sk_buff *skb;
  832. unsigned int ulen, copied;
  833. int peeked;
  834. int err;
  835. int is_udplite = IS_UDPLITE(sk);
  836. /*
  837. * Check any passed addresses
  838. */
  839. if (addr_len)
  840. *addr_len = sizeof(*sin);
  841. if (flags & MSG_ERRQUEUE)
  842. return ip_recv_error(sk, msg, len);
  843. try_again:
  844. skb = __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0),
  845. &peeked, &err);
  846. if (!skb)
  847. goto out;
  848. ulen = skb->len - sizeof(struct udphdr);
  849. copied = len;
  850. if (copied > ulen)
  851. copied = ulen;
  852. else if (copied < ulen)
  853. msg->msg_flags |= MSG_TRUNC;
  854. /*
  855. * If checksum is needed at all, try to do it while copying the
  856. * data. If the data is truncated, or if we only want a partial
  857. * coverage checksum (UDP-Lite), do it before the copy.
  858. */
  859. if (copied < ulen || UDP_SKB_CB(skb)->partial_cov) {
  860. if (udp_lib_checksum_complete(skb))
  861. goto csum_copy_err;
  862. }
  863. if (skb_csum_unnecessary(skb))
  864. err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr),
  865. msg->msg_iov, copied);
  866. else {
  867. err = skb_copy_and_csum_datagram_iovec(skb,
  868. sizeof(struct udphdr),
  869. msg->msg_iov);
  870. if (err == -EINVAL)
  871. goto csum_copy_err;
  872. }
  873. if (err)
  874. goto out_free;
  875. if (!peeked)
  876. UDP_INC_STATS_USER(sock_net(sk),
  877. UDP_MIB_INDATAGRAMS, is_udplite);
  878. sock_recv_ts_and_drops(msg, sk, skb);
  879. /* Copy the address. */
  880. if (sin) {
  881. sin->sin_family = AF_INET;
  882. sin->sin_port = udp_hdr(skb)->source;
  883. sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
  884. memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
  885. }
  886. if (inet->cmsg_flags)
  887. ip_cmsg_recv(msg, skb);
  888. err = copied;
  889. if (flags & MSG_TRUNC)
  890. err = ulen;
  891. out_free:
  892. lock_sock(sk);
  893. skb_free_datagram(sk, skb);
  894. release_sock(sk);
  895. out:
  896. return err;
  897. csum_copy_err:
  898. lock_sock(sk);
  899. if (!skb_kill_datagram(sk, skb, flags))
  900. UDP_INC_STATS_USER(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
  901. release_sock(sk);
  902. if (noblock)
  903. return -EAGAIN;
  904. goto try_again;
  905. }
  906. int udp_disconnect(struct sock *sk, int flags)
  907. {
  908. struct inet_sock *inet = inet_sk(sk);
  909. /*
  910. * 1003.1g - break association.
  911. */
  912. sk->sk_state = TCP_CLOSE;
  913. inet->daddr = 0;
  914. inet->dport = 0;
  915. sk->sk_bound_dev_if = 0;
  916. if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK))
  917. inet_reset_saddr(sk);
  918. if (!(sk->sk_userlocks & SOCK_BINDPORT_LOCK)) {
  919. sk->sk_prot->unhash(sk);
  920. inet->sport = 0;
  921. }
  922. sk_dst_reset(sk);
  923. return 0;
  924. }
  925. EXPORT_SYMBOL(udp_disconnect);
  926. void udp_lib_unhash(struct sock *sk)
  927. {
  928. if (sk_hashed(sk)) {
  929. struct udp_table *udptable = sk->sk_prot->h.udp_table;
  930. struct udp_hslot *hslot = udp_hashslot(udptable, sock_net(sk),
  931. sk->sk_hash);
  932. spin_lock_bh(&hslot->lock);
  933. if (sk_nulls_del_node_init_rcu(sk)) {
  934. inet_sk(sk)->num = 0;
  935. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
  936. }
  937. spin_unlock_bh(&hslot->lock);
  938. }
  939. }
  940. EXPORT_SYMBOL(udp_lib_unhash);
  941. static int __udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
  942. {
  943. int rc = sock_queue_rcv_skb(sk, skb);
  944. if (rc < 0) {
  945. int is_udplite = IS_UDPLITE(sk);
  946. /* Note that an ENOMEM error is charged twice */
  947. if (rc == -ENOMEM)
  948. UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_RCVBUFERRORS,
  949. is_udplite);
  950. UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
  951. kfree_skb(skb);
  952. return -1;
  953. }
  954. return 0;
  955. }
  956. /* returns:
  957. * -1: error
  958. * 0: success
  959. * >0: "udp encap" protocol resubmission
  960. *
  961. * Note that in the success and error cases, the skb is assumed to
  962. * have either been requeued or freed.
  963. */
  964. int udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
  965. {
  966. struct udp_sock *up = udp_sk(sk);
  967. int rc;
  968. int is_udplite = IS_UDPLITE(sk);
  969. /*
  970. * Charge it to the socket, dropping if the queue is full.
  971. */
  972. if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb))
  973. goto drop;
  974. nf_reset(skb);
  975. if (up->encap_type) {
  976. /*
  977. * This is an encapsulation socket so pass the skb to
  978. * the socket's udp_encap_rcv() hook. Otherwise, just
  979. * fall through and pass this up the UDP socket.
  980. * up->encap_rcv() returns the following value:
  981. * =0 if skb was successfully passed to the encap
  982. * handler or was discarded by it.
  983. * >0 if skb should be passed on to UDP.
  984. * <0 if skb should be resubmitted as proto -N
  985. */
  986. /* if we're overly short, let UDP handle it */
  987. if (skb->len > sizeof(struct udphdr) &&
  988. up->encap_rcv != NULL) {
  989. int ret;
  990. ret = (*up->encap_rcv)(sk, skb);
  991. if (ret <= 0) {
  992. UDP_INC_STATS_BH(sock_net(sk),
  993. UDP_MIB_INDATAGRAMS,
  994. is_udplite);
  995. return -ret;
  996. }
  997. }
  998. /* FALLTHROUGH -- it's a UDP Packet */
  999. }
  1000. /*
  1001. * UDP-Lite specific tests, ignored on UDP sockets
  1002. */
  1003. if ((is_udplite & UDPLITE_RECV_CC) && UDP_SKB_CB(skb)->partial_cov) {
  1004. /*
  1005. * MIB statistics other than incrementing the error count are
  1006. * disabled for the following two types of errors: these depend
  1007. * on the application settings, not on the functioning of the
  1008. * protocol stack as such.
  1009. *
  1010. * RFC 3828 here recommends (sec 3.3): "There should also be a
  1011. * way ... to ... at least let the receiving application block
  1012. * delivery of packets with coverage values less than a value
  1013. * provided by the application."
  1014. */
  1015. if (up->pcrlen == 0) { /* full coverage was set */
  1016. LIMIT_NETDEBUG(KERN_WARNING "UDPLITE: partial coverage "
  1017. "%d while full coverage %d requested\n",
  1018. UDP_SKB_CB(skb)->cscov, skb->len);
  1019. goto drop;
  1020. }
  1021. /* The next case involves violating the min. coverage requested
  1022. * by the receiver. This is subtle: if receiver wants x and x is
  1023. * greater than the buffersize/MTU then receiver will complain
  1024. * that it wants x while sender emits packets of smaller size y.
  1025. * Therefore the above ...()->partial_cov statement is essential.
  1026. */
  1027. if (UDP_SKB_CB(skb)->cscov < up->pcrlen) {
  1028. LIMIT_NETDEBUG(KERN_WARNING
  1029. "UDPLITE: coverage %d too small, need min %d\n",
  1030. UDP_SKB_CB(skb)->cscov, up->pcrlen);
  1031. goto drop;
  1032. }
  1033. }
  1034. if (sk->sk_filter) {
  1035. if (udp_lib_checksum_complete(skb))
  1036. goto drop;
  1037. }
  1038. rc = 0;
  1039. bh_lock_sock(sk);
  1040. if (!sock_owned_by_user(sk))
  1041. rc = __udp_queue_rcv_skb(sk, skb);
  1042. else
  1043. sk_add_backlog(sk, skb);
  1044. bh_unlock_sock(sk);
  1045. return rc;
  1046. drop:
  1047. UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
  1048. kfree_skb(skb);
  1049. return -1;
  1050. }
  1051. /*
  1052. * Multicasts and broadcasts go to each listener.
  1053. *
  1054. * Note: called only from the BH handler context,
  1055. * so we don't need to lock the hashes.
  1056. */
  1057. static int __udp4_lib_mcast_deliver(struct net *net, struct sk_buff *skb,
  1058. struct udphdr *uh,
  1059. __be32 saddr, __be32 daddr,
  1060. struct udp_table *udptable)
  1061. {
  1062. struct sock *sk;
  1063. struct udp_hslot *hslot = udp_hashslot(udptable, net, ntohs(uh->dest));
  1064. int dif;
  1065. spin_lock(&hslot->lock);
  1066. sk = sk_nulls_head(&hslot->head);
  1067. dif = skb->dev->ifindex;
  1068. sk = udp_v4_mcast_next(net, sk, uh->dest, daddr, uh->source, saddr, dif);
  1069. if (sk) {
  1070. struct sock *sknext = NULL;
  1071. do {
  1072. struct sk_buff *skb1 = skb;
  1073. sknext = udp_v4_mcast_next(net, sk_nulls_next(sk), uh->dest,
  1074. daddr, uh->source, saddr,
  1075. dif);
  1076. if (sknext)
  1077. skb1 = skb_clone(skb, GFP_ATOMIC);
  1078. if (skb1) {
  1079. int ret = udp_queue_rcv_skb(sk, skb1);
  1080. if (ret > 0)
  1081. /* we should probably re-process instead
  1082. * of dropping packets here. */
  1083. kfree_skb(skb1);
  1084. }
  1085. sk = sknext;
  1086. } while (sknext);
  1087. } else
  1088. consume_skb(skb);
  1089. spin_unlock(&hslot->lock);
  1090. return 0;
  1091. }
  1092. /* Initialize UDP checksum. If exited with zero value (success),
  1093. * CHECKSUM_UNNECESSARY means, that no more checks are required.
  1094. * Otherwise, csum completion requires chacksumming packet body,
  1095. * including udp header and folding it to skb->csum.
  1096. */
  1097. static inline int udp4_csum_init(struct sk_buff *skb, struct udphdr *uh,
  1098. int proto)
  1099. {
  1100. const struct iphdr *iph;
  1101. int err;
  1102. UDP_SKB_CB(skb)->partial_cov = 0;
  1103. UDP_SKB_CB(skb)->cscov = skb->len;
  1104. if (proto == IPPROTO_UDPLITE) {
  1105. err = udplite_checksum_init(skb, uh);
  1106. if (err)
  1107. return err;
  1108. }
  1109. iph = ip_hdr(skb);
  1110. if (uh->check == 0) {
  1111. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1112. } else if (skb->ip_summed == CHECKSUM_COMPLETE) {
  1113. if (!csum_tcpudp_magic(iph->saddr, iph->daddr, skb->len,
  1114. proto, skb->csum))
  1115. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1116. }
  1117. if (!skb_csum_unnecessary(skb))
  1118. skb->csum = csum_tcpudp_nofold(iph->saddr, iph->daddr,
  1119. skb->len, proto, 0);
  1120. /* Probably, we should checksum udp header (it should be in cache
  1121. * in any case) and data in tiny packets (< rx copybreak).
  1122. */
  1123. return 0;
  1124. }
  1125. /*
  1126. * All we need to do is get the socket, and then do a checksum.
  1127. */
  1128. int __udp4_lib_rcv(struct sk_buff *skb, struct udp_table *udptable,
  1129. int proto)
  1130. {
  1131. struct sock *sk;
  1132. struct udphdr *uh;
  1133. unsigned short ulen;
  1134. struct rtable *rt = skb_rtable(skb);
  1135. __be32 saddr, daddr;
  1136. struct net *net = dev_net(skb->dev);
  1137. /*
  1138. * Validate the packet.
  1139. */
  1140. if (!pskb_may_pull(skb, sizeof(struct udphdr)))
  1141. goto drop; /* No space for header. */
  1142. uh = udp_hdr(skb);
  1143. ulen = ntohs(uh->len);
  1144. if (ulen > skb->len)
  1145. goto short_packet;
  1146. if (proto == IPPROTO_UDP) {
  1147. /* UDP validates ulen. */
  1148. if (ulen < sizeof(*uh) || pskb_trim_rcsum(skb, ulen))
  1149. goto short_packet;
  1150. uh = udp_hdr(skb);
  1151. }
  1152. if (udp4_csum_init(skb, uh, proto))
  1153. goto csum_error;
  1154. saddr = ip_hdr(skb)->saddr;
  1155. daddr = ip_hdr(skb)->daddr;
  1156. if (rt->rt_flags & (RTCF_BROADCAST|RTCF_MULTICAST))
  1157. return __udp4_lib_mcast_deliver(net, skb, uh,
  1158. saddr, daddr, udptable);
  1159. sk = __udp4_lib_lookup_skb(skb, uh->source, uh->dest, udptable);
  1160. if (sk != NULL) {
  1161. int ret = udp_queue_rcv_skb(sk, skb);
  1162. sock_put(sk);
  1163. /* a return value > 0 means to resubmit the input, but
  1164. * it wants the return to be -protocol, or 0
  1165. */
  1166. if (ret > 0)
  1167. return -ret;
  1168. return 0;
  1169. }
  1170. if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
  1171. goto drop;
  1172. nf_reset(skb);
  1173. /* No socket. Drop packet silently, if checksum is wrong */
  1174. if (udp_lib_checksum_complete(skb))
  1175. goto csum_error;
  1176. UDP_INC_STATS_BH(net, UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE);
  1177. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
  1178. /*
  1179. * Hmm. We got an UDP packet to a port to which we
  1180. * don't wanna listen. Ignore it.
  1181. */
  1182. kfree_skb(skb);
  1183. return 0;
  1184. short_packet:
  1185. LIMIT_NETDEBUG(KERN_DEBUG "UDP%s: short packet: From %pI4:%u %d/%d to %pI4:%u\n",
  1186. proto == IPPROTO_UDPLITE ? "-Lite" : "",
  1187. &saddr,
  1188. ntohs(uh->source),
  1189. ulen,
  1190. skb->len,
  1191. &daddr,
  1192. ntohs(uh->dest));
  1193. goto drop;
  1194. csum_error:
  1195. /*
  1196. * RFC1122: OK. Discards the bad packet silently (as far as
  1197. * the network is concerned, anyway) as per 4.1.3.4 (MUST).
  1198. */
  1199. LIMIT_NETDEBUG(KERN_DEBUG "UDP%s: bad checksum. From %pI4:%u to %pI4:%u ulen %d\n",
  1200. proto == IPPROTO_UDPLITE ? "-Lite" : "",
  1201. &saddr,
  1202. ntohs(uh->source),
  1203. &daddr,
  1204. ntohs(uh->dest),
  1205. ulen);
  1206. drop:
  1207. UDP_INC_STATS_BH(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE);
  1208. kfree_skb(skb);
  1209. return 0;
  1210. }
  1211. int udp_rcv(struct sk_buff *skb)
  1212. {
  1213. return __udp4_lib_rcv(skb, &udp_table, IPPROTO_UDP);
  1214. }
  1215. void udp_destroy_sock(struct sock *sk)
  1216. {
  1217. lock_sock(sk);
  1218. udp_flush_pending_frames(sk);
  1219. release_sock(sk);
  1220. }
  1221. /*
  1222. * Socket option code for UDP
  1223. */
  1224. int udp_lib_setsockopt(struct sock *sk, int level, int optname,
  1225. char __user *optval, unsigned int optlen,
  1226. int (*push_pending_frames)(struct sock *))
  1227. {
  1228. struct udp_sock *up = udp_sk(sk);
  1229. int val;
  1230. int err = 0;
  1231. int is_udplite = IS_UDPLITE(sk);
  1232. if (optlen < sizeof(int))
  1233. return -EINVAL;
  1234. if (get_user(val, (int __user *)optval))
  1235. return -EFAULT;
  1236. switch (optname) {
  1237. case UDP_CORK:
  1238. if (val != 0) {
  1239. up->corkflag = 1;
  1240. } else {
  1241. up->corkflag = 0;
  1242. lock_sock(sk);
  1243. (*push_pending_frames)(sk);
  1244. release_sock(sk);
  1245. }
  1246. break;
  1247. case UDP_ENCAP:
  1248. switch (val) {
  1249. case 0:
  1250. case UDP_ENCAP_ESPINUDP:
  1251. case UDP_ENCAP_ESPINUDP_NON_IKE:
  1252. up->encap_rcv = xfrm4_udp_encap_rcv;
  1253. /* FALLTHROUGH */
  1254. case UDP_ENCAP_L2TPINUDP:
  1255. up->encap_type = val;
  1256. break;
  1257. default:
  1258. err = -ENOPROTOOPT;
  1259. break;
  1260. }
  1261. break;
  1262. /*
  1263. * UDP-Lite's partial checksum coverage (RFC 3828).
  1264. */
  1265. /* The sender sets actual checksum coverage length via this option.
  1266. * The case coverage > packet length is handled by send module. */
  1267. case UDPLITE_SEND_CSCOV:
  1268. if (!is_udplite) /* Disable the option on UDP sockets */
  1269. return -ENOPROTOOPT;
  1270. if (val != 0 && val < 8) /* Illegal coverage: use default (8) */
  1271. val = 8;
  1272. else if (val > USHORT_MAX)
  1273. val = USHORT_MAX;
  1274. up->pcslen = val;
  1275. up->pcflag |= UDPLITE_SEND_CC;
  1276. break;
  1277. /* The receiver specifies a minimum checksum coverage value. To make
  1278. * sense, this should be set to at least 8 (as done below). If zero is
  1279. * used, this again means full checksum coverage. */
  1280. case UDPLITE_RECV_CSCOV:
  1281. if (!is_udplite) /* Disable the option on UDP sockets */
  1282. return -ENOPROTOOPT;
  1283. if (val != 0 && val < 8) /* Avoid silly minimal values. */
  1284. val = 8;
  1285. else if (val > USHORT_MAX)
  1286. val = USHORT_MAX;
  1287. up->pcrlen = val;
  1288. up->pcflag |= UDPLITE_RECV_CC;
  1289. break;
  1290. default:
  1291. err = -ENOPROTOOPT;
  1292. break;
  1293. }
  1294. return err;
  1295. }
  1296. EXPORT_SYMBOL(udp_lib_setsockopt);
  1297. int udp_setsockopt(struct sock *sk, int level, int optname,
  1298. char __user *optval, unsigned int optlen)
  1299. {
  1300. if (level == SOL_UDP || level == SOL_UDPLITE)
  1301. return udp_lib_setsockopt(sk, level, optname, optval, optlen,
  1302. udp_push_pending_frames);
  1303. return ip_setsockopt(sk, level, optname, optval, optlen);
  1304. }
  1305. #ifdef CONFIG_COMPAT
  1306. int compat_udp_setsockopt(struct sock *sk, int level, int optname,
  1307. char __user *optval, unsigned int optlen)
  1308. {
  1309. if (level == SOL_UDP || level == SOL_UDPLITE)
  1310. return udp_lib_setsockopt(sk, level, optname, optval, optlen,
  1311. udp_push_pending_frames);
  1312. return compat_ip_setsockopt(sk, level, optname, optval, optlen);
  1313. }
  1314. #endif
  1315. int udp_lib_getsockopt(struct sock *sk, int level, int optname,
  1316. char __user *optval, int __user *optlen)
  1317. {
  1318. struct udp_sock *up = udp_sk(sk);
  1319. int val, len;
  1320. if (get_user(len, optlen))
  1321. return -EFAULT;
  1322. len = min_t(unsigned int, len, sizeof(int));
  1323. if (len < 0)
  1324. return -EINVAL;
  1325. switch (optname) {
  1326. case UDP_CORK:
  1327. val = up->corkflag;
  1328. break;
  1329. case UDP_ENCAP:
  1330. val = up->encap_type;
  1331. break;
  1332. /* The following two cannot be changed on UDP sockets, the return is
  1333. * always 0 (which corresponds to the full checksum coverage of UDP). */
  1334. case UDPLITE_SEND_CSCOV:
  1335. val = up->pcslen;
  1336. break;
  1337. case UDPLITE_RECV_CSCOV:
  1338. val = up->pcrlen;
  1339. break;
  1340. default:
  1341. return -ENOPROTOOPT;
  1342. }
  1343. if (put_user(len, optlen))
  1344. return -EFAULT;
  1345. if (copy_to_user(optval, &val, len))
  1346. return -EFAULT;
  1347. return 0;
  1348. }
  1349. EXPORT_SYMBOL(udp_lib_getsockopt);
  1350. int udp_getsockopt(struct sock *sk, int level, int optname,
  1351. char __user *optval, int __user *optlen)
  1352. {
  1353. if (level == SOL_UDP || level == SOL_UDPLITE)
  1354. return udp_lib_getsockopt(sk, level, optname, optval, optlen);
  1355. return ip_getsockopt(sk, level, optname, optval, optlen);
  1356. }
  1357. #ifdef CONFIG_COMPAT
  1358. int compat_udp_getsockopt(struct sock *sk, int level, int optname,
  1359. char __user *optval, int __user *optlen)
  1360. {
  1361. if (level == SOL_UDP || level == SOL_UDPLITE)
  1362. return udp_lib_getsockopt(sk, level, optname, optval, optlen);
  1363. return compat_ip_getsockopt(sk, level, optname, optval, optlen);
  1364. }
  1365. #endif
  1366. /**
  1367. * udp_poll - wait for a UDP event.
  1368. * @file - file struct
  1369. * @sock - socket
  1370. * @wait - poll table
  1371. *
  1372. * This is same as datagram poll, except for the special case of
  1373. * blocking sockets. If application is using a blocking fd
  1374. * and a packet with checksum error is in the queue;
  1375. * then it could get return from select indicating data available
  1376. * but then block when reading it. Add special case code
  1377. * to work around these arguably broken applications.
  1378. */
  1379. unsigned int udp_poll(struct file *file, struct socket *sock, poll_table *wait)
  1380. {
  1381. unsigned int mask = datagram_poll(file, sock, wait);
  1382. struct sock *sk = sock->sk;
  1383. /* Check for false positives due to checksum errors */
  1384. if ((mask & POLLRDNORM) && !(file->f_flags & O_NONBLOCK) &&
  1385. !(sk->sk_shutdown & RCV_SHUTDOWN) && !first_packet_length(sk))
  1386. mask &= ~(POLLIN | POLLRDNORM);
  1387. return mask;
  1388. }
  1389. EXPORT_SYMBOL(udp_poll);
  1390. struct proto udp_prot = {
  1391. .name = "UDP",
  1392. .owner = THIS_MODULE,
  1393. .close = udp_lib_close,
  1394. .connect = ip4_datagram_connect,
  1395. .disconnect = udp_disconnect,
  1396. .ioctl = udp_ioctl,
  1397. .destroy = udp_destroy_sock,
  1398. .setsockopt = udp_setsockopt,
  1399. .getsockopt = udp_getsockopt,
  1400. .sendmsg = udp_sendmsg,
  1401. .recvmsg = udp_recvmsg,
  1402. .sendpage = udp_sendpage,
  1403. .backlog_rcv = __udp_queue_rcv_skb,
  1404. .hash = udp_lib_hash,
  1405. .unhash = udp_lib_unhash,
  1406. .get_port = udp_v4_get_port,
  1407. .memory_allocated = &udp_memory_allocated,
  1408. .sysctl_mem = sysctl_udp_mem,
  1409. .sysctl_wmem = &sysctl_udp_wmem_min,
  1410. .sysctl_rmem = &sysctl_udp_rmem_min,
  1411. .obj_size = sizeof(struct udp_sock),
  1412. .slab_flags = SLAB_DESTROY_BY_RCU,
  1413. .h.udp_table = &udp_table,
  1414. #ifdef CONFIG_COMPAT
  1415. .compat_setsockopt = compat_udp_setsockopt,
  1416. .compat_getsockopt = compat_udp_getsockopt,
  1417. #endif
  1418. };
  1419. EXPORT_SYMBOL(udp_prot);
  1420. /* ------------------------------------------------------------------------ */
  1421. #ifdef CONFIG_PROC_FS
  1422. static struct sock *udp_get_first(struct seq_file *seq, int start)
  1423. {
  1424. struct sock *sk;
  1425. struct udp_iter_state *state = seq->private;
  1426. struct net *net = seq_file_net(seq);
  1427. for (state->bucket = start; state->bucket <= state->udp_table->mask;
  1428. ++state->bucket) {
  1429. struct hlist_nulls_node *node;
  1430. struct udp_hslot *hslot = &state->udp_table->hash[state->bucket];
  1431. if (hlist_nulls_empty(&hslot->head))
  1432. continue;
  1433. spin_lock_bh(&hslot->lock);
  1434. sk_nulls_for_each(sk, node, &hslot->head) {
  1435. if (!net_eq(sock_net(sk), net))
  1436. continue;
  1437. if (sk->sk_family == state->family)
  1438. goto found;
  1439. }
  1440. spin_unlock_bh(&hslot->lock);
  1441. }
  1442. sk = NULL;
  1443. found:
  1444. return sk;
  1445. }
  1446. static struct sock *udp_get_next(struct seq_file *seq, struct sock *sk)
  1447. {
  1448. struct udp_iter_state *state = seq->private;
  1449. struct net *net = seq_file_net(seq);
  1450. do {
  1451. sk = sk_nulls_next(sk);
  1452. } while (sk && (!net_eq(sock_net(sk), net) || sk->sk_family != state->family));
  1453. if (!sk) {
  1454. if (state->bucket <= state->udp_table->mask)
  1455. spin_unlock_bh(&state->udp_table->hash[state->bucket].lock);
  1456. return udp_get_first(seq, state->bucket + 1);
  1457. }
  1458. return sk;
  1459. }
  1460. static struct sock *udp_get_idx(struct seq_file *seq, loff_t pos)
  1461. {
  1462. struct sock *sk = udp_get_first(seq, 0);
  1463. if (sk)
  1464. while (pos && (sk = udp_get_next(seq, sk)) != NULL)
  1465. --pos;
  1466. return pos ? NULL : sk;
  1467. }
  1468. static void *udp_seq_start(struct seq_file *seq, loff_t *pos)
  1469. {
  1470. struct udp_iter_state *state = seq->private;
  1471. state->bucket = MAX_UDP_PORTS;
  1472. return *pos ? udp_get_idx(seq, *pos-1) : SEQ_START_TOKEN;
  1473. }
  1474. static void *udp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  1475. {
  1476. struct sock *sk;
  1477. if (v == SEQ_START_TOKEN)
  1478. sk = udp_get_idx(seq, 0);
  1479. else
  1480. sk = udp_get_next(seq, v);
  1481. ++*pos;
  1482. return sk;
  1483. }
  1484. static void udp_seq_stop(struct seq_file *seq, void *v)
  1485. {
  1486. struct udp_iter_state *state = seq->private;
  1487. if (state->bucket <= state->udp_table->mask)
  1488. spin_unlock_bh(&state->udp_table->hash[state->bucket].lock);
  1489. }
  1490. static int udp_seq_open(struct inode *inode, struct file *file)
  1491. {
  1492. struct udp_seq_afinfo *afinfo = PDE(inode)->data;
  1493. struct udp_iter_state *s;
  1494. int err;
  1495. err = seq_open_net(inode, file, &afinfo->seq_ops,
  1496. sizeof(struct udp_iter_state));
  1497. if (err < 0)
  1498. return err;
  1499. s = ((struct seq_file *)file->private_data)->private;
  1500. s->family = afinfo->family;
  1501. s->udp_table = afinfo->udp_table;
  1502. return err;
  1503. }
  1504. /* ------------------------------------------------------------------------ */
  1505. int udp_proc_register(struct net *net, struct udp_seq_afinfo *afinfo)
  1506. {
  1507. struct proc_dir_entry *p;
  1508. int rc = 0;
  1509. afinfo->seq_fops.open = udp_seq_open;
  1510. afinfo->seq_fops.read = seq_read;
  1511. afinfo->seq_fops.llseek = seq_lseek;
  1512. afinfo->seq_fops.release = seq_release_net;
  1513. afinfo->seq_ops.start = udp_seq_start;
  1514. afinfo->seq_ops.next = udp_seq_next;
  1515. afinfo->seq_ops.stop = udp_seq_stop;
  1516. p = proc_create_data(afinfo->name, S_IRUGO, net->proc_net,
  1517. &afinfo->seq_fops, afinfo);
  1518. if (!p)
  1519. rc = -ENOMEM;
  1520. return rc;
  1521. }
  1522. EXPORT_SYMBOL(udp_proc_register);
  1523. void udp_proc_unregister(struct net *net, struct udp_seq_afinfo *afinfo)
  1524. {
  1525. proc_net_remove(net, afinfo->name);
  1526. }
  1527. EXPORT_SYMBOL(udp_proc_unregister);
  1528. /* ------------------------------------------------------------------------ */
  1529. static void udp4_format_sock(struct sock *sp, struct seq_file *f,
  1530. int bucket, int *len)
  1531. {
  1532. struct inet_sock *inet = inet_sk(sp);
  1533. __be32 dest = inet->daddr;
  1534. __be32 src = inet->rcv_saddr;
  1535. __u16 destp = ntohs(inet->dport);
  1536. __u16 srcp = ntohs(inet->sport);
  1537. seq_printf(f, "%5d: %08X:%04X %08X:%04X"
  1538. " %02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p %d%n",
  1539. bucket, src, srcp, dest, destp, sp->sk_state,
  1540. sk_wmem_alloc_get(sp),
  1541. sk_rmem_alloc_get(sp),
  1542. 0, 0L, 0, sock_i_uid(sp), 0, sock_i_ino(sp),
  1543. atomic_read(&sp->sk_refcnt), sp,
  1544. atomic_read(&sp->sk_drops), len);
  1545. }
  1546. int udp4_seq_show(struct seq_file *seq, void *v)
  1547. {
  1548. if (v == SEQ_START_TOKEN)
  1549. seq_printf(seq, "%-127s\n",
  1550. " sl local_address rem_address st tx_queue "
  1551. "rx_queue tr tm->when retrnsmt uid timeout "
  1552. "inode ref pointer drops");
  1553. else {
  1554. struct udp_iter_state *state = seq->private;
  1555. int len;
  1556. udp4_format_sock(v, seq, state->bucket, &len);
  1557. seq_printf(seq, "%*s\n", 127 - len, "");
  1558. }
  1559. return 0;
  1560. }
  1561. /* ------------------------------------------------------------------------ */
  1562. static struct udp_seq_afinfo udp4_seq_afinfo = {
  1563. .name = "udp",
  1564. .family = AF_INET,
  1565. .udp_table = &udp_table,
  1566. .seq_fops = {
  1567. .owner = THIS_MODULE,
  1568. },
  1569. .seq_ops = {
  1570. .show = udp4_seq_show,
  1571. },
  1572. };
  1573. static int udp4_proc_init_net(struct net *net)
  1574. {
  1575. return udp_proc_register(net, &udp4_seq_afinfo);
  1576. }
  1577. static void udp4_proc_exit_net(struct net *net)
  1578. {
  1579. udp_proc_unregister(net, &udp4_seq_afinfo);
  1580. }
  1581. static struct pernet_operations udp4_net_ops = {
  1582. .init = udp4_proc_init_net,
  1583. .exit = udp4_proc_exit_net,
  1584. };
  1585. int __init udp4_proc_init(void)
  1586. {
  1587. return register_pernet_subsys(&udp4_net_ops);
  1588. }
  1589. void udp4_proc_exit(void)
  1590. {
  1591. unregister_pernet_subsys(&udp4_net_ops);
  1592. }
  1593. #endif /* CONFIG_PROC_FS */
  1594. static __initdata unsigned long uhash_entries;
  1595. static int __init set_uhash_entries(char *str)
  1596. {
  1597. if (!str)
  1598. return 0;
  1599. uhash_entries = simple_strtoul(str, &str, 0);
  1600. if (uhash_entries && uhash_entries < UDP_HTABLE_SIZE_MIN)
  1601. uhash_entries = UDP_HTABLE_SIZE_MIN;
  1602. return 1;
  1603. }
  1604. __setup("uhash_entries=", set_uhash_entries);
  1605. void __init udp_table_init(struct udp_table *table, const char *name)
  1606. {
  1607. unsigned int i;
  1608. if (!CONFIG_BASE_SMALL)
  1609. table->hash = alloc_large_system_hash(name,
  1610. sizeof(struct udp_hslot),
  1611. uhash_entries,
  1612. 21, /* one slot per 2 MB */
  1613. 0,
  1614. &table->log,
  1615. &table->mask,
  1616. 64 * 1024);
  1617. /*
  1618. * Make sure hash table has the minimum size
  1619. */
  1620. if (CONFIG_BASE_SMALL || table->mask < UDP_HTABLE_SIZE_MIN - 1) {
  1621. table->hash = kmalloc(UDP_HTABLE_SIZE_MIN *
  1622. sizeof(struct udp_hslot), GFP_KERNEL);
  1623. if (!table->hash)
  1624. panic(name);
  1625. table->log = ilog2(UDP_HTABLE_SIZE_MIN);
  1626. table->mask = UDP_HTABLE_SIZE_MIN - 1;
  1627. }
  1628. for (i = 0; i <= table->mask; i++) {
  1629. INIT_HLIST_NULLS_HEAD(&table->hash[i].head, i);
  1630. spin_lock_init(&table->hash[i].lock);
  1631. }
  1632. }
  1633. void __init udp_init(void)
  1634. {
  1635. unsigned long nr_pages, limit;
  1636. udp_table_init(&udp_table, "UDP");
  1637. /* Set the pressure threshold up by the same strategy of TCP. It is a
  1638. * fraction of global memory that is up to 1/2 at 256 MB, decreasing
  1639. * toward zero with the amount of memory, with a floor of 128 pages.
  1640. */
  1641. nr_pages = totalram_pages - totalhigh_pages;
  1642. limit = min(nr_pages, 1UL<<(28-PAGE_SHIFT)) >> (20-PAGE_SHIFT);
  1643. limit = (limit * (nr_pages >> (20-PAGE_SHIFT))) >> (PAGE_SHIFT-11);
  1644. limit = max(limit, 128UL);
  1645. sysctl_udp_mem[0] = limit / 4 * 3;
  1646. sysctl_udp_mem[1] = limit;
  1647. sysctl_udp_mem[2] = sysctl_udp_mem[0] * 2;
  1648. sysctl_udp_rmem_min = SK_MEM_QUANTUM;
  1649. sysctl_udp_wmem_min = SK_MEM_QUANTUM;
  1650. }
  1651. int udp4_ufo_send_check(struct sk_buff *skb)
  1652. {
  1653. const struct iphdr *iph;
  1654. struct udphdr *uh;
  1655. if (!pskb_may_pull(skb, sizeof(*uh)))
  1656. return -EINVAL;
  1657. iph = ip_hdr(skb);
  1658. uh = udp_hdr(skb);
  1659. uh->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr, skb->len,
  1660. IPPROTO_UDP, 0);
  1661. skb->csum_start = skb_transport_header(skb) - skb->head;
  1662. skb->csum_offset = offsetof(struct udphdr, check);
  1663. skb->ip_summed = CHECKSUM_PARTIAL;
  1664. return 0;
  1665. }
  1666. struct sk_buff *udp4_ufo_fragment(struct sk_buff *skb, int features)
  1667. {
  1668. struct sk_buff *segs = ERR_PTR(-EINVAL);
  1669. unsigned int mss;
  1670. int offset;
  1671. __wsum csum;
  1672. mss = skb_shinfo(skb)->gso_size;
  1673. if (unlikely(skb->len <= mss))
  1674. goto out;
  1675. if (skb_gso_ok(skb, features | NETIF_F_GSO_ROBUST)) {
  1676. /* Packet is from an untrusted source, reset gso_segs. */
  1677. int type = skb_shinfo(skb)->gso_type;
  1678. if (unlikely(type & ~(SKB_GSO_UDP | SKB_GSO_DODGY) ||
  1679. !(type & (SKB_GSO_UDP))))
  1680. goto out;
  1681. skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(skb->len, mss);
  1682. segs = NULL;
  1683. goto out;
  1684. }
  1685. /* Do software UFO. Complete and fill in the UDP checksum as HW cannot
  1686. * do checksum of UDP packets sent as multiple IP fragments.
  1687. */
  1688. offset = skb->csum_start - skb_headroom(skb);
  1689. csum = skb_checksum(skb, offset, skb->len - offset, 0);
  1690. offset += skb->csum_offset;
  1691. *(__sum16 *)(skb->data + offset) = csum_fold(csum);
  1692. skb->ip_summed = CHECKSUM_NONE;
  1693. /* Fragment the skb. IP headers of the fragments are updated in
  1694. * inet_gso_segment()
  1695. */
  1696. segs = skb_segment(skb, features);
  1697. out:
  1698. return segs;
  1699. }