udp.c 49 KB

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