900-unaligned_access_hacks.patch 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  1. From: Felix Fietkau <[email protected]>
  2. Subject: [PATCH] ar71xx: fix unaligned access in a few more places
  3. SVN-Revision: 35130
  4. ---
  5. arch/mips/include/asm/checksum.h | 83 +++---------------
  6. include/uapi/linux/ip.h | 2 +-
  7. include/uapi/linux/ipv6.h | 2 +-
  8. include/uapi/linux/tcp.h | 4 ++--
  9. include/uapi/linux/udp.h | 2 +-
  10. net/netfilter/nf_conntrack_core.c | 4 ++--
  11. include/uapi/linux/icmp.h | 2 +-
  12. include/uapi/linux/in6.h | 2 +-
  13. net/ipv6/tcp_ipv6.c | 9 +++--
  14. net/ipv6/datagram.c | 6 ++--
  15. net/ipv6/exthdrs.c | 2 +-
  16. include/linux/types.h | 5 +++
  17. net/ipv4/af_inet.c | 4 ++--
  18. net/ipv4/tcp_output.c | 69 +++++++++--------
  19. include/uapi/linux/igmp.h | 8 +++---
  20. net/core/flow_dissector.c | 2 +-
  21. include/uapi/linux/icmpv6.h | 2 +-
  22. include/net/ndisc.h | 10 ++++----
  23. net/sched/cls_u32.c | 6 +++---
  24. net/ipv6/ip6_offload.c | 2 +-
  25. include/net/addrconf.h | 2 +-
  26. include/net/inet_ecn.h | 4 ++--
  27. include/net/ipv6.h | 23 +++++----
  28. include/net/secure_seq.h | 1 +
  29. include/uapi/linux/in.h | 2 +-
  30. net/ipv6/ip6_fib.h | 2 +-
  31. net/netfilter/nf_conntrack_proto_tcp.c | 2 +-
  32. net/xfrm/xfrm_input.c | 4 ++--
  33. net/ipv4/tcp_input.c | 12 ++++---
  34. include/uapi/linux/if_pppox.h | 1 +
  35. net/ipv6/netfilter/nf_log_ipv6.c | 4 ++--
  36. include/net/neighbour.h | 6 +++--
  37. include/uapi/linux/netfilter_arp/arp_tables.h | 2 +-
  38. net/core/utils.c | 10 +++++--
  39. include/linux/etherdevice.h | 11 ++++---
  40. net/ipv4/tcp_offload.c | 6 +++---
  41. net/ipv6/netfilter/ip6table_mangle.c | 4 ++--
  42. 37 file changed, 171 insertions(+), 141 deletions(-)
  43. --- a/arch/mips/include/asm/checksum.h
  44. +++ b/arch/mips/include/asm/checksum.h
  45. @@ -100,26 +100,30 @@ static inline __sum16 ip_fast_csum(const
  46. const unsigned int *stop = word + ihl;
  47. unsigned int csum;
  48. int carry;
  49. + unsigned int w;
  50. - csum = word[0];
  51. - csum += word[1];
  52. - carry = (csum < word[1]);
  53. + csum = net_hdr_word(word++);
  54. +
  55. + w = net_hdr_word(word++);
  56. + csum += w;
  57. + carry = (csum < w);
  58. csum += carry;
  59. - csum += word[2];
  60. - carry = (csum < word[2]);
  61. + w = net_hdr_word(word++);
  62. + csum += w;
  63. + carry = (csum < w);
  64. csum += carry;
  65. - csum += word[3];
  66. - carry = (csum < word[3]);
  67. + w = net_hdr_word(word++);
  68. + csum += w;
  69. + carry = (csum < w);
  70. csum += carry;
  71. - word += 4;
  72. do {
  73. - csum += *word;
  74. - carry = (csum < *word);
  75. + w = net_hdr_word(word++);
  76. + csum += w;
  77. + carry = (csum < w);
  78. csum += carry;
  79. - word++;
  80. } while (word != stop);
  81. return csum_fold(csum);
  82. @@ -179,74 +183,6 @@ static inline __sum16 ip_compute_csum(co
  83. return csum_fold(csum_partial(buff, len, 0));
  84. }
  85. -#define _HAVE_ARCH_IPV6_CSUM
  86. -static __inline__ __sum16 csum_ipv6_magic(const struct in6_addr *saddr,
  87. - const struct in6_addr *daddr,
  88. - __u32 len, __u8 proto,
  89. - __wsum sum)
  90. -{
  91. - __wsum tmp;
  92. -
  93. - __asm__(
  94. - " .set push # csum_ipv6_magic\n"
  95. - " .set noreorder \n"
  96. - " .set noat \n"
  97. - " addu %0, %5 # proto (long in network byte order)\n"
  98. - " sltu $1, %0, %5 \n"
  99. - " addu %0, $1 \n"
  100. -
  101. - " addu %0, %6 # csum\n"
  102. - " sltu $1, %0, %6 \n"
  103. - " lw %1, 0(%2) # four words source address\n"
  104. - " addu %0, $1 \n"
  105. - " addu %0, %1 \n"
  106. - " sltu $1, %0, %1 \n"
  107. -
  108. - " lw %1, 4(%2) \n"
  109. - " addu %0, $1 \n"
  110. - " addu %0, %1 \n"
  111. - " sltu $1, %0, %1 \n"
  112. -
  113. - " lw %1, 8(%2) \n"
  114. - " addu %0, $1 \n"
  115. - " addu %0, %1 \n"
  116. - " sltu $1, %0, %1 \n"
  117. -
  118. - " lw %1, 12(%2) \n"
  119. - " addu %0, $1 \n"
  120. - " addu %0, %1 \n"
  121. - " sltu $1, %0, %1 \n"
  122. -
  123. - " lw %1, 0(%3) \n"
  124. - " addu %0, $1 \n"
  125. - " addu %0, %1 \n"
  126. - " sltu $1, %0, %1 \n"
  127. -
  128. - " lw %1, 4(%3) \n"
  129. - " addu %0, $1 \n"
  130. - " addu %0, %1 \n"
  131. - " sltu $1, %0, %1 \n"
  132. -
  133. - " lw %1, 8(%3) \n"
  134. - " addu %0, $1 \n"
  135. - " addu %0, %1 \n"
  136. - " sltu $1, %0, %1 \n"
  137. -
  138. - " lw %1, 12(%3) \n"
  139. - " addu %0, $1 \n"
  140. - " addu %0, %1 \n"
  141. - " sltu $1, %0, %1 \n"
  142. -
  143. - " addu %0, $1 # Add final carry\n"
  144. - " .set pop"
  145. - : "=&r" (sum), "=&r" (tmp)
  146. - : "r" (saddr), "r" (daddr),
  147. - "0" (htonl(len)), "r" (htonl(proto)), "r" (sum)
  148. - : "memory");
  149. -
  150. - return csum_fold(sum);
  151. -}
  152. -
  153. #include <asm-generic/checksum.h>
  154. #endif /* CONFIG_GENERIC_CSUM */
  155. --- a/include/uapi/linux/ip.h
  156. +++ b/include/uapi/linux/ip.h
  157. @@ -106,7 +106,7 @@ struct iphdr {
  158. __be32 daddr;
  159. );
  160. /*The options start here. */
  161. -};
  162. +} __attribute__((packed, aligned(2)));
  163. struct ip_auth_hdr {
  164. --- a/include/uapi/linux/ipv6.h
  165. +++ b/include/uapi/linux/ipv6.h
  166. @@ -135,7 +135,7 @@ struct ipv6hdr {
  167. struct in6_addr saddr;
  168. struct in6_addr daddr;
  169. );
  170. -};
  171. +} __attribute__((packed, aligned(2)));
  172. /* index values for the variables in ipv6_devconf */
  173. --- a/include/uapi/linux/tcp.h
  174. +++ b/include/uapi/linux/tcp.h
  175. @@ -55,7 +55,7 @@ struct tcphdr {
  176. __be16 window;
  177. __sum16 check;
  178. __be16 urg_ptr;
  179. -};
  180. +} __attribute__((packed, aligned(2)));
  181. /*
  182. * The union cast uses a gcc extension to avoid aliasing problems
  183. @@ -65,7 +65,7 @@ struct tcphdr {
  184. union tcp_word_hdr {
  185. struct tcphdr hdr;
  186. __be32 words[5];
  187. -};
  188. +} __attribute__((packed, aligned(2)));
  189. #define tcp_flag_word(tp) (((union tcp_word_hdr *)(tp))->words[3])
  190. --- a/include/uapi/linux/udp.h
  191. +++ b/include/uapi/linux/udp.h
  192. @@ -25,7 +25,7 @@ struct udphdr {
  193. __be16 dest;
  194. __be16 len;
  195. __sum16 check;
  196. -};
  197. +} __attribute__((packed, aligned(2)));
  198. /* UDP socket options */
  199. #define UDP_CORK 1 /* Never send partially complete segments */
  200. --- a/net/netfilter/nf_conntrack_core.c
  201. +++ b/net/netfilter/nf_conntrack_core.c
  202. @@ -308,8 +308,8 @@ nf_ct_get_tuple(const struct sk_buff *sk
  203. switch (l3num) {
  204. case NFPROTO_IPV4:
  205. - tuple->src.u3.ip = ap[0];
  206. - tuple->dst.u3.ip = ap[1];
  207. + tuple->src.u3.ip = net_hdr_word(ap++);
  208. + tuple->dst.u3.ip = net_hdr_word(ap);
  209. break;
  210. case NFPROTO_IPV6:
  211. memcpy(tuple->src.u3.ip6, ap, sizeof(tuple->src.u3.ip6));
  212. --- a/include/uapi/linux/icmp.h
  213. +++ b/include/uapi/linux/icmp.h
  214. @@ -102,7 +102,7 @@ struct icmphdr {
  215. } frag;
  216. __u8 reserved[4];
  217. } un;
  218. -};
  219. +} __attribute__((packed, aligned(2)));
  220. /*
  221. --- a/include/uapi/linux/in6.h
  222. +++ b/include/uapi/linux/in6.h
  223. @@ -43,7 +43,7 @@ struct in6_addr {
  224. #define s6_addr16 in6_u.u6_addr16
  225. #define s6_addr32 in6_u.u6_addr32
  226. #endif
  227. -};
  228. +} __attribute__((packed, aligned(2)));
  229. #endif /* __UAPI_DEF_IN6_ADDR */
  230. #if __UAPI_DEF_SOCKADDR_IN6
  231. --- a/net/ipv6/tcp_ipv6.c
  232. +++ b/net/ipv6/tcp_ipv6.c
  233. @@ -35,6 +35,7 @@
  234. #include <linux/ipsec.h>
  235. #include <linux/times.h>
  236. #include <linux/slab.h>
  237. +#include <asm/unaligned.h>
  238. #include <linux/uaccess.h>
  239. #include <linux/ipv6.h>
  240. #include <linux/icmpv6.h>
  241. @@ -901,10 +902,10 @@ static void tcp_v6_send_response(const s
  242. topt = (__be32 *)(t1 + 1);
  243. if (tsecr) {
  244. - *topt++ = htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) |
  245. - (TCPOPT_TIMESTAMP << 8) | TCPOLEN_TIMESTAMP);
  246. - *topt++ = htonl(tsval);
  247. - *topt++ = htonl(tsecr);
  248. + put_unaligned_be32((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) |
  249. + (TCPOPT_TIMESTAMP << 8) | TCPOLEN_TIMESTAMP, topt++);
  250. + put_unaligned_be32(tsval, topt++);
  251. + put_unaligned_be32(tsecr, topt++);
  252. }
  253. if (mrst)
  254. --- a/include/linux/ipv6.h
  255. +++ b/include/linux/ipv6.h
  256. @@ -6,6 +6,7 @@
  257. #define ipv6_optlen(p) (((p)->hdrlen+1) << 3)
  258. #define ipv6_authlen(p) (((p)->hdrlen+2) << 2)
  259. +
  260. /*
  261. * This structure contains configuration options per IPv6 link.
  262. */
  263. --- a/net/ipv6/datagram.c
  264. +++ b/net/ipv6/datagram.c
  265. @@ -497,7 +497,7 @@ int ipv6_recv_error(struct sock *sk, str
  266. ipv6_iface_scope_id(&sin->sin6_addr,
  267. IP6CB(skb)->iif);
  268. } else {
  269. - ipv6_addr_set_v4mapped(*(__be32 *)(nh + serr->addr_offset),
  270. + ipv6_addr_set_v4mapped(net_hdr_word(nh + serr->addr_offset),
  271. &sin->sin6_addr);
  272. sin->sin6_scope_id = 0;
  273. }
  274. @@ -851,12 +851,12 @@ int ip6_datagram_send_ctl(struct net *ne
  275. }
  276. if (fl6->flowlabel&IPV6_FLOWINFO_MASK) {
  277. - if ((fl6->flowlabel^*(__be32 *)CMSG_DATA(cmsg))&~IPV6_FLOWINFO_MASK) {
  278. + if ((fl6->flowlabel^net_hdr_word(CMSG_DATA(cmsg)))&~IPV6_FLOWINFO_MASK) {
  279. err = -EINVAL;
  280. goto exit_f;
  281. }
  282. }
  283. - fl6->flowlabel = IPV6_FLOWINFO_MASK & *(__be32 *)CMSG_DATA(cmsg);
  284. + fl6->flowlabel = IPV6_FLOWINFO_MASK & net_hdr_word(CMSG_DATA(cmsg));
  285. break;
  286. case IPV6_2292HOPOPTS:
  287. --- a/net/ipv6/exthdrs.c
  288. +++ b/net/ipv6/exthdrs.c
  289. @@ -1003,7 +1003,7 @@ static bool ipv6_hop_jumbo(struct sk_buf
  290. goto drop;
  291. }
  292. - pkt_len = ntohl(*(__be32 *)(nh + optoff + 2));
  293. + pkt_len = ntohl(net_hdr_word(nh + optoff + 2));
  294. if (pkt_len <= IPV6_MAXPLEN) {
  295. icmpv6_param_prob_reason(skb, ICMPV6_HDR_FIELD, optoff + 2,
  296. SKB_DROP_REASON_IP_INHDR);
  297. --- a/include/linux/types.h
  298. +++ b/include/linux/types.h
  299. @@ -232,5 +232,11 @@ typedef void (*swap_func_t)(void *a, voi
  300. typedef int (*cmp_r_func_t)(const void *a, const void *b, const void *priv);
  301. typedef int (*cmp_func_t)(const void *a, const void *b);
  302. +struct net_hdr_word {
  303. + u32 words[1];
  304. +} __attribute__((packed, aligned(2)));
  305. +
  306. +#define net_hdr_word(_p) (((struct net_hdr_word *) (_p))->words[0])
  307. +
  308. #endif /* __ASSEMBLY__ */
  309. #endif /* _LINUX_TYPES_H */
  310. --- a/net/ipv4/af_inet.c
  311. +++ b/net/ipv4/af_inet.c
  312. @@ -1515,8 +1515,8 @@ struct sk_buff *inet_gro_receive(struct
  313. if (unlikely(ip_fast_csum((u8 *)iph, 5)))
  314. goto out;
  315. - id = ntohl(*(__be32 *)&iph->id);
  316. - flush = (u16)((ntohl(*(__be32 *)iph) ^ skb_gro_len(skb)) | (id & ~IP_DF));
  317. + id = ntohl(net_hdr_word(&iph->id));
  318. + flush = (u16)((ntohl(net_hdr_word(iph)) ^ skb_gro_len(skb)) | (id & ~IP_DF));
  319. id >>= 16;
  320. list_for_each_entry(p, head, list) {
  321. --- a/net/ipv4/tcp_output.c
  322. +++ b/net/ipv4/tcp_output.c
  323. @@ -612,48 +612,53 @@ static void tcp_options_write(struct tcp
  324. u16 options = opts->options; /* mungable copy */
  325. if (unlikely(OPTION_MD5 & options)) {
  326. - *ptr++ = htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) |
  327. - (TCPOPT_MD5SIG << 8) | TCPOLEN_MD5SIG);
  328. + net_hdr_word(ptr++) =
  329. + htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) |
  330. + (TCPOPT_MD5SIG << 8) | TCPOLEN_MD5SIG);
  331. /* overload cookie hash location */
  332. opts->hash_location = (__u8 *)ptr;
  333. ptr += 4;
  334. }
  335. if (unlikely(opts->mss)) {
  336. - *ptr++ = htonl((TCPOPT_MSS << 24) |
  337. - (TCPOLEN_MSS << 16) |
  338. - opts->mss);
  339. + net_hdr_word(ptr++) =
  340. + htonl((TCPOPT_MSS << 24) | (TCPOLEN_MSS << 16) |
  341. + opts->mss);
  342. }
  343. if (likely(OPTION_TS & options)) {
  344. if (unlikely(OPTION_SACK_ADVERTISE & options)) {
  345. - *ptr++ = htonl((TCPOPT_SACK_PERM << 24) |
  346. - (TCPOLEN_SACK_PERM << 16) |
  347. - (TCPOPT_TIMESTAMP << 8) |
  348. - TCPOLEN_TIMESTAMP);
  349. + net_hdr_word(ptr++) =
  350. + htonl((TCPOPT_SACK_PERM << 24) |
  351. + (TCPOLEN_SACK_PERM << 16) |
  352. + (TCPOPT_TIMESTAMP << 8) |
  353. + TCPOLEN_TIMESTAMP);
  354. options &= ~OPTION_SACK_ADVERTISE;
  355. } else {
  356. - *ptr++ = htonl((TCPOPT_NOP << 24) |
  357. - (TCPOPT_NOP << 16) |
  358. - (TCPOPT_TIMESTAMP << 8) |
  359. - TCPOLEN_TIMESTAMP);
  360. + net_hdr_word(ptr++) =
  361. + htonl((TCPOPT_NOP << 24) |
  362. + (TCPOPT_NOP << 16) |
  363. + (TCPOPT_TIMESTAMP << 8) |
  364. + TCPOLEN_TIMESTAMP);
  365. }
  366. - *ptr++ = htonl(opts->tsval);
  367. - *ptr++ = htonl(opts->tsecr);
  368. + net_hdr_word(ptr++) = htonl(opts->tsval);
  369. + net_hdr_word(ptr++) = htonl(opts->tsecr);
  370. }
  371. if (unlikely(OPTION_SACK_ADVERTISE & options)) {
  372. - *ptr++ = htonl((TCPOPT_NOP << 24) |
  373. - (TCPOPT_NOP << 16) |
  374. - (TCPOPT_SACK_PERM << 8) |
  375. - TCPOLEN_SACK_PERM);
  376. + net_hdr_word(ptr++) =
  377. + htonl((TCPOPT_NOP << 24) |
  378. + (TCPOPT_NOP << 16) |
  379. + (TCPOPT_SACK_PERM << 8) |
  380. + TCPOLEN_SACK_PERM);
  381. }
  382. if (unlikely(OPTION_WSCALE & options)) {
  383. - *ptr++ = htonl((TCPOPT_NOP << 24) |
  384. - (TCPOPT_WINDOW << 16) |
  385. - (TCPOLEN_WINDOW << 8) |
  386. - opts->ws);
  387. + net_hdr_word(ptr++) =
  388. + htonl((TCPOPT_NOP << 24) |
  389. + (TCPOPT_WINDOW << 16) |
  390. + (TCPOLEN_WINDOW << 8) |
  391. + opts->ws);
  392. }
  393. if (unlikely(opts->num_sack_blocks)) {
  394. @@ -661,16 +666,17 @@ static void tcp_options_write(struct tcp
  395. tp->duplicate_sack : tp->selective_acks;
  396. int this_sack;
  397. - *ptr++ = htonl((TCPOPT_NOP << 24) |
  398. - (TCPOPT_NOP << 16) |
  399. - (TCPOPT_SACK << 8) |
  400. - (TCPOLEN_SACK_BASE + (opts->num_sack_blocks *
  401. + net_hdr_word(ptr++) =
  402. + htonl((TCPOPT_NOP << 24) |
  403. + (TCPOPT_NOP << 16) |
  404. + (TCPOPT_SACK << 8) |
  405. + (TCPOLEN_SACK_BASE + (opts->num_sack_blocks *
  406. TCPOLEN_SACK_PERBLOCK)));
  407. for (this_sack = 0; this_sack < opts->num_sack_blocks;
  408. ++this_sack) {
  409. - *ptr++ = htonl(sp[this_sack].start_seq);
  410. - *ptr++ = htonl(sp[this_sack].end_seq);
  411. + net_hdr_word(ptr++) = htonl(sp[this_sack].start_seq);
  412. + net_hdr_word(ptr++) = htonl(sp[this_sack].end_seq);
  413. }
  414. tp->rx_opt.dsack = 0;
  415. @@ -683,13 +689,14 @@ static void tcp_options_write(struct tcp
  416. if (foc->exp) {
  417. len = TCPOLEN_EXP_FASTOPEN_BASE + foc->len;
  418. - *ptr = htonl((TCPOPT_EXP << 24) | (len << 16) |
  419. + net_hdr_word(ptr) =
  420. + htonl((TCPOPT_EXP << 24) | (len << 16) |
  421. TCPOPT_FASTOPEN_MAGIC);
  422. p += TCPOLEN_EXP_FASTOPEN_BASE;
  423. } else {
  424. len = TCPOLEN_FASTOPEN_BASE + foc->len;
  425. - *p++ = TCPOPT_FASTOPEN;
  426. - *p++ = len;
  427. + net_hdr_word(p++) = TCPOPT_FASTOPEN;
  428. + net_hdr_word(p++) = len;
  429. }
  430. memcpy(p, foc->val, foc->len);
  431. --- a/include/uapi/linux/igmp.h
  432. +++ b/include/uapi/linux/igmp.h
  433. @@ -33,7 +33,7 @@ struct igmphdr {
  434. __u8 code; /* For newer IGMP */
  435. __sum16 csum;
  436. __be32 group;
  437. -};
  438. +} __attribute__((packed, aligned(2)));
  439. /* V3 group record types [grec_type] */
  440. #define IGMPV3_MODE_IS_INCLUDE 1
  441. @@ -49,7 +49,7 @@ struct igmpv3_grec {
  442. __be16 grec_nsrcs;
  443. __be32 grec_mca;
  444. __be32 grec_src[];
  445. -};
  446. +} __attribute__((packed, aligned(2)));
  447. struct igmpv3_report {
  448. __u8 type;
  449. @@ -58,7 +58,7 @@ struct igmpv3_report {
  450. __be16 resv2;
  451. __be16 ngrec;
  452. struct igmpv3_grec grec[];
  453. -};
  454. +} __attribute__((packed, aligned(2)));
  455. struct igmpv3_query {
  456. __u8 type;
  457. @@ -79,7 +79,7 @@ struct igmpv3_query {
  458. __u8 qqic;
  459. __be16 nsrcs;
  460. __be32 srcs[];
  461. -};
  462. +} __attribute__((packed, aligned(2)));
  463. #define IGMP_HOST_MEMBERSHIP_QUERY 0x11 /* From RFC1112 */
  464. #define IGMP_HOST_MEMBERSHIP_REPORT 0x12 /* Ditto */
  465. --- a/net/core/flow_dissector.c
  466. +++ b/net/core/flow_dissector.c
  467. @@ -131,7 +131,7 @@ __be32 __skb_flow_get_ports(const struct
  468. ports = __skb_header_pointer(skb, thoff + poff,
  469. sizeof(_ports), data, hlen, &_ports);
  470. if (ports)
  471. - return *ports;
  472. + return (__be32)net_hdr_word(ports);
  473. }
  474. return 0;
  475. --- a/include/uapi/linux/icmpv6.h
  476. +++ b/include/uapi/linux/icmpv6.h
  477. @@ -78,7 +78,7 @@ struct icmp6hdr {
  478. #define icmp6_addrconf_other icmp6_dataun.u_nd_ra.other
  479. #define icmp6_rt_lifetime icmp6_dataun.u_nd_ra.rt_lifetime
  480. #define icmp6_router_pref icmp6_dataun.u_nd_ra.router_pref
  481. -};
  482. +} __attribute__((packed, aligned(2)));
  483. #define ICMPV6_ROUTER_PREF_LOW 0x3
  484. --- a/include/net/ndisc.h
  485. +++ b/include/net/ndisc.h
  486. @@ -93,7 +93,7 @@ struct ra_msg {
  487. struct icmp6hdr icmph;
  488. __be32 reachable_time;
  489. __be32 retrans_timer;
  490. -};
  491. +} __attribute__((packed, aligned(2)));
  492. struct rd_msg {
  493. struct icmp6hdr icmph;
  494. @@ -372,10 +372,10 @@ static inline u32 ndisc_hashfn(const voi
  495. {
  496. const u32 *p32 = pkey;
  497. - return (((p32[0] ^ hash32_ptr(dev)) * hash_rnd[0]) +
  498. - (p32[1] * hash_rnd[1]) +
  499. - (p32[2] * hash_rnd[2]) +
  500. - (p32[3] * hash_rnd[3]));
  501. + return (((net_hdr_word(&p32[0]) ^ hash32_ptr(dev)) * hash_rnd[0]) +
  502. + (net_hdr_word(&p32[1]) * hash_rnd[1]) +
  503. + (net_hdr_word(&p32[2]) * hash_rnd[2]) +
  504. + (net_hdr_word(&p32[3]) * hash_rnd[3]));
  505. }
  506. static inline struct neighbour *__ipv6_neigh_lookup_noref(struct net_device *dev, const void *pkey)
  507. --- a/net/sched/cls_u32.c
  508. +++ b/net/sched/cls_u32.c
  509. @@ -155,7 +155,7 @@ next_knode:
  510. data = skb_header_pointer(skb, toff, 4, &hdata);
  511. if (!data)
  512. goto out;
  513. - if ((*data ^ key->val) & key->mask) {
  514. + if ((net_hdr_word(data) ^ key->val) & key->mask) {
  515. n = rcu_dereference_bh(n->next);
  516. goto next_knode;
  517. }
  518. @@ -206,8 +206,8 @@ check_terminal:
  519. &hdata);
  520. if (!data)
  521. goto out;
  522. - sel = ht->divisor & u32_hash_fold(*data, &n->sel,
  523. - n->fshift);
  524. + sel = ht->divisor & u32_hash_fold(net_hdr_word(data),
  525. + &n->sel, n->fshift);
  526. }
  527. if (!(n->sel.flags & (TC_U32_VAROFFSET | TC_U32_OFFSET | TC_U32_EAT)))
  528. goto next_ht;
  529. --- a/net/ipv6/ip6_offload.c
  530. +++ b/net/ipv6/ip6_offload.c
  531. @@ -259,7 +259,7 @@ INDIRECT_CALLABLE_SCOPE struct sk_buff *
  532. continue;
  533. iph2 = (struct ipv6hdr *)(p->data + off);
  534. - first_word = *(__be32 *)iph ^ *(__be32 *)iph2;
  535. + first_word = net_hdr_word(iph) ^ net_hdr_word(iph2);
  536. /* All fields must match except length and Traffic Class.
  537. * XXX skbs on the gro_list have all been parsed and pulled
  538. --- a/include/net/addrconf.h
  539. +++ b/include/net/addrconf.h
  540. @@ -52,7 +52,7 @@ struct prefix_info {
  541. __be32 reserved2;
  542. struct in6_addr prefix;
  543. -};
  544. +} __attribute__((packed, aligned(2)));
  545. /* rfc4861 4.6.2: IPv6 PIO is 32 bytes in size */
  546. static_assert(sizeof(struct prefix_info) == 32);
  547. --- a/include/net/inet_ecn.h
  548. +++ b/include/net/inet_ecn.h
  549. @@ -138,9 +138,9 @@ static inline int IP6_ECN_set_ce(struct
  550. if (INET_ECN_is_not_ect(ipv6_get_dsfield(iph)))
  551. return 0;
  552. - from = *(__be32 *)iph;
  553. + from = net_hdr_word(iph);
  554. to = from | htonl(INET_ECN_CE << 20);
  555. - *(__be32 *)iph = to;
  556. + net_hdr_word(iph) = to;
  557. if (skb->ip_summed == CHECKSUM_COMPLETE)
  558. skb->csum = csum_add(csum_sub(skb->csum, (__force __wsum)from),
  559. (__force __wsum)to);
  560. --- a/include/net/ipv6.h
  561. +++ b/include/net/ipv6.h
  562. @@ -149,7 +149,7 @@ struct frag_hdr {
  563. __u8 reserved;
  564. __be16 frag_off;
  565. __be32 identification;
  566. -};
  567. +} __attribute__((packed, aligned(2)));
  568. /*
  569. * Jumbo payload option, as described in RFC 2675 2.
  570. @@ -615,8 +615,8 @@ static inline void __ipv6_addr_set_half(
  571. }
  572. #endif
  573. #endif
  574. - addr[0] = wh;
  575. - addr[1] = wl;
  576. + net_hdr_word(&addr[0]) = wh;
  577. + net_hdr_word(&addr[1]) = wl;
  578. }
  579. static inline void ipv6_addr_set(struct in6_addr *addr,
  580. @@ -675,6 +675,8 @@ static inline bool ipv6_prefix_equal(con
  581. const __be32 *a1 = addr1->s6_addr32;
  582. const __be32 *a2 = addr2->s6_addr32;
  583. unsigned int pdw, pbi;
  584. + /* Used for last <32-bit fraction of prefix */
  585. + u32 pbia1, pbia2;
  586. /* check complete u32 in prefix */
  587. pdw = prefixlen >> 5;
  588. @@ -683,7 +685,9 @@ static inline bool ipv6_prefix_equal(con
  589. /* check incomplete u32 in prefix */
  590. pbi = prefixlen & 0x1f;
  591. - if (pbi && ((a1[pdw] ^ a2[pdw]) & htonl((0xffffffff) << (32 - pbi))))
  592. + pbia1 = net_hdr_word(&a1[pdw]);
  593. + pbia2 = net_hdr_word(&a2[pdw]);
  594. + if (pbi && ((pbia1 ^ pbia2) & htonl((0xffffffff) << (32 - pbi))))
  595. return false;
  596. return true;
  597. @@ -805,13 +809,13 @@ static inline void ipv6_addr_set_v4mappe
  598. */
  599. static inline int __ipv6_addr_diff32(const void *token1, const void *token2, int addrlen)
  600. {
  601. - const __be32 *a1 = token1, *a2 = token2;
  602. + const struct in6_addr *a1 = token1, *a2 = token2;
  603. int i;
  604. addrlen >>= 2;
  605. for (i = 0; i < addrlen; i++) {
  606. - __be32 xb = a1[i] ^ a2[i];
  607. + __be32 xb = a1->s6_addr32[i] ^ a2->s6_addr32[i];
  608. if (xb)
  609. return i * 32 + 31 - __fls(ntohl(xb));
  610. }
  611. @@ -1005,17 +1009,18 @@ static inline u32 ip6_multipath_hash_fie
  612. static inline void ip6_flow_hdr(struct ipv6hdr *hdr, unsigned int tclass,
  613. __be32 flowlabel)
  614. {
  615. - *(__be32 *)hdr = htonl(0x60000000 | (tclass << 20)) | flowlabel;
  616. + net_hdr_word((__be32 *)hdr) =
  617. + htonl(0x60000000 | (tclass << 20)) | flowlabel;
  618. }
  619. static inline __be32 ip6_flowinfo(const struct ipv6hdr *hdr)
  620. {
  621. - return *(__be32 *)hdr & IPV6_FLOWINFO_MASK;
  622. + return net_hdr_word((__be32 *)hdr) & IPV6_FLOWINFO_MASK;
  623. }
  624. static inline __be32 ip6_flowlabel(const struct ipv6hdr *hdr)
  625. {
  626. - return *(__be32 *)hdr & IPV6_FLOWLABEL_MASK;
  627. + return net_hdr_word((__be32 *)hdr) & IPV6_FLOWLABEL_MASK;
  628. }
  629. static inline u8 ip6_tclass(__be32 flowinfo)
  630. --- a/include/net/secure_seq.h
  631. +++ b/include/net/secure_seq.h
  632. @@ -3,6 +3,7 @@
  633. #define _NET_SECURE_SEQ
  634. #include <linux/types.h>
  635. +#include <linux/in6.h>
  636. struct net;
  637. --- a/include/uapi/linux/in.h
  638. +++ b/include/uapi/linux/in.h
  639. @@ -91,7 +91,7 @@ enum {
  640. /* Internet address. */
  641. struct in_addr {
  642. __be32 s_addr;
  643. -};
  644. +} __attribute__((packed, aligned(2)));
  645. #endif
  646. #define IP_TOS 1
  647. --- a/net/ipv6/ip6_fib.c
  648. +++ b/net/ipv6/ip6_fib.c
  649. @@ -142,7 +142,7 @@ static __be32 addr_bit_set(const void *t
  650. * See include/asm-generic/bitops/le.h.
  651. */
  652. return (__force __be32)(1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)) &
  653. - addr[fn_bit >> 5];
  654. + net_hdr_word(&addr[fn_bit >> 5]);
  655. }
  656. struct fib6_info *fib6_info_alloc(gfp_t gfp_flags, bool with_fib6_nh)
  657. --- a/net/netfilter/nf_conntrack_proto_tcp.c
  658. +++ b/net/netfilter/nf_conntrack_proto_tcp.c
  659. @@ -406,7 +406,7 @@ static void tcp_sack(const struct sk_buf
  660. /* Fast path for timestamp-only option */
  661. if (length == TCPOLEN_TSTAMP_ALIGNED
  662. - && *(__be32 *)ptr == htonl((TCPOPT_NOP << 24)
  663. + && net_hdr_word(ptr) == htonl((TCPOPT_NOP << 24)
  664. | (TCPOPT_NOP << 16)
  665. | (TCPOPT_TIMESTAMP << 8)
  666. | TCPOLEN_TIMESTAMP))
  667. --- a/net/xfrm/xfrm_input.c
  668. +++ b/net/xfrm/xfrm_input.c
  669. @@ -168,8 +168,8 @@ int xfrm_parse_spi(struct sk_buff *skb,
  670. if (!pskb_may_pull(skb, hlen))
  671. return -EINVAL;
  672. - *spi = *(__be32 *)(skb_transport_header(skb) + offset);
  673. - *seq = *(__be32 *)(skb_transport_header(skb) + offset_seq);
  674. + *spi = net_hdr_word(skb_transport_header(skb) + offset);
  675. + *seq = net_hdr_word(skb_transport_header(skb) + offset_seq);
  676. return 0;
  677. }
  678. EXPORT_SYMBOL(xfrm_parse_spi);
  679. --- a/net/ipv4/tcp_input.c
  680. +++ b/net/ipv4/tcp_input.c
  681. @@ -4183,14 +4183,16 @@ static bool tcp_parse_aligned_timestamp(
  682. {
  683. const __be32 *ptr = (const __be32 *)(th + 1);
  684. - if (*ptr == htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16)
  685. - | (TCPOPT_TIMESTAMP << 8) | TCPOLEN_TIMESTAMP)) {
  686. + if (net_hdr_word(ptr) ==
  687. + htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) |
  688. + (TCPOPT_TIMESTAMP << 8) | TCPOLEN_TIMESTAMP)) {
  689. tp->rx_opt.saw_tstamp = 1;
  690. ++ptr;
  691. - tp->rx_opt.rcv_tsval = ntohl(*ptr);
  692. + tp->rx_opt.rcv_tsval = get_unaligned_be32(ptr);
  693. ++ptr;
  694. - if (*ptr)
  695. - tp->rx_opt.rcv_tsecr = ntohl(*ptr) - tp->tsoffset;
  696. + if (net_hdr_word(ptr))
  697. + tp->rx_opt.rcv_tsecr = get_unaligned_be32(ptr) -
  698. + tp->tsoffset;
  699. else
  700. tp->rx_opt.rcv_tsecr = 0;
  701. return true;
  702. --- a/include/uapi/linux/if_pppox.h
  703. +++ b/include/uapi/linux/if_pppox.h
  704. @@ -51,6 +51,7 @@ struct pppoe_addr {
  705. */
  706. struct pptp_addr {
  707. __u16 call_id;
  708. + __u16 pad;
  709. struct in_addr sin_addr;
  710. };
  711. --- a/include/net/neighbour.h
  712. +++ b/include/net/neighbour.h
  713. @@ -286,8 +286,10 @@ static inline bool neigh_key_eq128(const
  714. const u32 *n32 = (const u32 *)n->primary_key;
  715. const u32 *p32 = pkey;
  716. - return ((n32[0] ^ p32[0]) | (n32[1] ^ p32[1]) |
  717. - (n32[2] ^ p32[2]) | (n32[3] ^ p32[3])) == 0;
  718. + return ((n32[0] ^ net_hdr_word(&p32[0])) |
  719. + (n32[1] ^ net_hdr_word(&p32[1])) |
  720. + (n32[2] ^ net_hdr_word(&p32[2])) |
  721. + (n32[3] ^ net_hdr_word(&p32[3]))) == 0;
  722. }
  723. static inline struct neighbour *___neigh_lookup_noref(
  724. --- a/include/uapi/linux/netfilter_arp/arp_tables.h
  725. +++ b/include/uapi/linux/netfilter_arp/arp_tables.h
  726. @@ -70,7 +70,7 @@ struct arpt_arp {
  727. __u8 flags;
  728. /* Inverse flags */
  729. __u16 invflags;
  730. -};
  731. +} __attribute__((aligned(4)));
  732. /* Values for "flag" field in struct arpt_ip (general arp structure).
  733. * No flags defined yet.
  734. --- a/net/core/utils.c
  735. +++ b/net/core/utils.c
  736. @@ -460,8 +460,14 @@ void inet_proto_csum_replace16(__sum16 *
  737. bool pseudohdr)
  738. {
  739. __be32 diff[] = {
  740. - ~from[0], ~from[1], ~from[2], ~from[3],
  741. - to[0], to[1], to[2], to[3],
  742. + ~net_hdr_word(&from[0]),
  743. + ~net_hdr_word(&from[1]),
  744. + ~net_hdr_word(&from[2]),
  745. + ~net_hdr_word(&from[3]),
  746. + net_hdr_word(&to[0]),
  747. + net_hdr_word(&to[1]),
  748. + net_hdr_word(&to[2]),
  749. + net_hdr_word(&to[3]),
  750. };
  751. if (skb->ip_summed != CHECKSUM_PARTIAL) {
  752. *sum = csum_fold(csum_partial(diff, sizeof(diff),
  753. --- a/include/linux/etherdevice.h
  754. +++ b/include/linux/etherdevice.h
  755. @@ -555,7 +555,7 @@ static inline bool is_etherdev_addr(cons
  756. * @b: Pointer to Ethernet header
  757. *
  758. * Compare two Ethernet headers, returns 0 if equal.
  759. - * This assumes that the network header (i.e., IP header) is 4-byte
  760. + * This assumes that the network header (i.e., IP header) is 2-byte
  761. * aligned OR the platform can handle unaligned access. This is the
  762. * case for all packets coming into netif_receive_skb or similar
  763. * entry points.
  764. @@ -578,11 +578,12 @@ static inline unsigned long compare_ethe
  765. fold |= *(unsigned long *)(a + 6) ^ *(unsigned long *)(b + 6);
  766. return fold;
  767. #else
  768. - u32 *a32 = (u32 *)((u8 *)a + 2);
  769. - u32 *b32 = (u32 *)((u8 *)b + 2);
  770. + const u16 *a16 = a;
  771. + const u16 *b16 = b;
  772. - return (*(u16 *)a ^ *(u16 *)b) | (a32[0] ^ b32[0]) |
  773. - (a32[1] ^ b32[1]) | (a32[2] ^ b32[2]);
  774. + return (a16[0] ^ b16[0]) | (a16[1] ^ b16[1]) | (a16[2] ^ b16[2]) |
  775. + (a16[3] ^ b16[3]) | (a16[4] ^ b16[4]) | (a16[5] ^ b16[5]) |
  776. + (a16[6] ^ b16[6]);
  777. #endif
  778. }
  779. --- a/net/ipv4/tcp_offload.c
  780. +++ b/net/ipv4/tcp_offload.c
  781. @@ -220,7 +220,7 @@ struct sk_buff *tcp_gro_receive(struct l
  782. th2 = tcp_hdr(p);
  783. - if (*(u32 *)&th->source ^ *(u32 *)&th2->source) {
  784. + if (net_hdr_word(&th->source) ^ net_hdr_word(&th2->source)) {
  785. NAPI_GRO_CB(p)->same_flow = 0;
  786. continue;
  787. }
  788. @@ -238,8 +238,8 @@ found:
  789. ~(TCP_FLAG_CWR | TCP_FLAG_FIN | TCP_FLAG_PSH));
  790. flush |= (__force int)(th->ack_seq ^ th2->ack_seq);
  791. for (i = sizeof(*th); i < thlen; i += 4)
  792. - flush |= *(u32 *)((u8 *)th + i) ^
  793. - *(u32 *)((u8 *)th2 + i);
  794. + flush |= net_hdr_word((u8 *)th + i) ^
  795. + net_hdr_word((u8 *)th2 + i);
  796. /* When we receive our second frame we can made a decision on if we
  797. * continue this flow as an atomic flow with a fixed ID or if we use
  798. --- a/net/ipv6/netfilter/ip6table_mangle.c
  799. +++ b/net/ipv6/netfilter/ip6table_mangle.c
  800. @@ -44,7 +44,7 @@ ip6t_mangle_out(void *priv, struct sk_bu
  801. hop_limit = ipv6_hdr(skb)->hop_limit;
  802. /* flowlabel and prio (includes version, which shouldn't change either */
  803. - flowlabel = *((u_int32_t *)ipv6_hdr(skb));
  804. + flowlabel = net_hdr_word(ipv6_hdr(skb));
  805. ret = ip6t_do_table(priv, skb, state);
  806. @@ -53,7 +53,7 @@ ip6t_mangle_out(void *priv, struct sk_bu
  807. !ipv6_addr_equal(&ipv6_hdr(skb)->daddr, &daddr) ||
  808. skb->mark != mark ||
  809. ipv6_hdr(skb)->hop_limit != hop_limit ||
  810. - flowlabel != *((u_int32_t *)ipv6_hdr(skb)))) {
  811. + flowlabel != net_hdr_word(ipv6_hdr(skb)))) {
  812. err = ip6_route_me_harder(state->net, state->sk, skb);
  813. if (err < 0)
  814. ret = NF_DROP_ERR(err);