udp.c 44 KB

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