udp.c 43 KB

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