br_netfilter.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027
  1. /*
  2. * Handle firewalling
  3. * Linux ethernet bridge
  4. *
  5. * Authors:
  6. * Lennert Buytenhek <buytenh@gnu.org>
  7. * Bart De Schuymer <bdschuym@pandora.be>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version
  12. * 2 of the License, or (at your option) any later version.
  13. *
  14. * Lennert dedicates this file to Kerstin Wurdinger.
  15. */
  16. #include <linux/module.h>
  17. #include <linux/kernel.h>
  18. #include <linux/slab.h>
  19. #include <linux/ip.h>
  20. #include <linux/netdevice.h>
  21. #include <linux/skbuff.h>
  22. #include <linux/if_arp.h>
  23. #include <linux/if_ether.h>
  24. #include <linux/if_vlan.h>
  25. #include <linux/if_pppox.h>
  26. #include <linux/ppp_defs.h>
  27. #include <linux/netfilter_bridge.h>
  28. #include <linux/netfilter_ipv4.h>
  29. #include <linux/netfilter_ipv6.h>
  30. #include <linux/netfilter_arp.h>
  31. #include <linux/in_route.h>
  32. #include <linux/inetdevice.h>
  33. #include <net/ip.h>
  34. #include <net/ipv6.h>
  35. #include <net/route.h>
  36. #include <asm/uaccess.h>
  37. #include "br_private.h"
  38. #ifdef CONFIG_SYSCTL
  39. #include <linux/sysctl.h>
  40. #endif
  41. #define skb_origaddr(skb) (((struct bridge_skb_cb *) \
  42. (skb->nf_bridge->data))->daddr.ipv4)
  43. #define store_orig_dstaddr(skb) (skb_origaddr(skb) = ip_hdr(skb)->daddr)
  44. #define dnat_took_place(skb) (skb_origaddr(skb) != ip_hdr(skb)->daddr)
  45. #ifdef CONFIG_SYSCTL
  46. static struct ctl_table_header *brnf_sysctl_header;
  47. static int brnf_call_iptables __read_mostly = 1;
  48. static int brnf_call_ip6tables __read_mostly = 1;
  49. static int brnf_call_arptables __read_mostly = 1;
  50. static int brnf_filter_vlan_tagged __read_mostly = 0;
  51. static int brnf_filter_pppoe_tagged __read_mostly = 0;
  52. static int brnf_pass_vlan_indev __read_mostly = 0;
  53. #else
  54. #define brnf_call_iptables 1
  55. #define brnf_call_ip6tables 1
  56. #define brnf_call_arptables 1
  57. #define brnf_filter_vlan_tagged 0
  58. #define brnf_filter_pppoe_tagged 0
  59. #define brnf_pass_vlan_indev 0
  60. #endif
  61. #define IS_IP(skb) \
  62. (!vlan_tx_tag_present(skb) && skb->protocol == htons(ETH_P_IP))
  63. #define IS_IPV6(skb) \
  64. (!vlan_tx_tag_present(skb) && skb->protocol == htons(ETH_P_IPV6))
  65. #define IS_ARP(skb) \
  66. (!vlan_tx_tag_present(skb) && skb->protocol == htons(ETH_P_ARP))
  67. static inline __be16 vlan_proto(const struct sk_buff *skb)
  68. {
  69. if (vlan_tx_tag_present(skb))
  70. return skb->protocol;
  71. else if (skb->protocol == htons(ETH_P_8021Q))
  72. return vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
  73. else
  74. return 0;
  75. }
  76. #define IS_VLAN_IP(skb) \
  77. (vlan_proto(skb) == htons(ETH_P_IP) && \
  78. brnf_filter_vlan_tagged)
  79. #define IS_VLAN_IPV6(skb) \
  80. (vlan_proto(skb) == htons(ETH_P_IPV6) && \
  81. brnf_filter_vlan_tagged)
  82. #define IS_VLAN_ARP(skb) \
  83. (vlan_proto(skb) == htons(ETH_P_ARP) && \
  84. brnf_filter_vlan_tagged)
  85. static inline __be16 pppoe_proto(const struct sk_buff *skb)
  86. {
  87. return *((__be16 *)(skb_mac_header(skb) + ETH_HLEN +
  88. sizeof(struct pppoe_hdr)));
  89. }
  90. #define IS_PPPOE_IP(skb) \
  91. (skb->protocol == htons(ETH_P_PPP_SES) && \
  92. pppoe_proto(skb) == htons(PPP_IP) && \
  93. brnf_filter_pppoe_tagged)
  94. #define IS_PPPOE_IPV6(skb) \
  95. (skb->protocol == htons(ETH_P_PPP_SES) && \
  96. pppoe_proto(skb) == htons(PPP_IPV6) && \
  97. brnf_filter_pppoe_tagged)
  98. static inline struct rtable *bridge_parent_rtable(const struct net_device *dev)
  99. {
  100. struct net_bridge_port *port;
  101. port = br_port_get_rcu(dev);
  102. return port ? &port->br->fake_rtable : NULL;
  103. }
  104. static inline struct net_device *bridge_parent(const struct net_device *dev)
  105. {
  106. struct net_bridge_port *port;
  107. port = br_port_get_rcu(dev);
  108. return port ? port->br->dev : NULL;
  109. }
  110. static inline struct nf_bridge_info *nf_bridge_alloc(struct sk_buff *skb)
  111. {
  112. skb->nf_bridge = kzalloc(sizeof(struct nf_bridge_info), GFP_ATOMIC);
  113. if (likely(skb->nf_bridge))
  114. atomic_set(&(skb->nf_bridge->use), 1);
  115. return skb->nf_bridge;
  116. }
  117. static inline struct nf_bridge_info *nf_bridge_unshare(struct sk_buff *skb)
  118. {
  119. struct nf_bridge_info *nf_bridge = skb->nf_bridge;
  120. if (atomic_read(&nf_bridge->use) > 1) {
  121. struct nf_bridge_info *tmp = nf_bridge_alloc(skb);
  122. if (tmp) {
  123. memcpy(tmp, nf_bridge, sizeof(struct nf_bridge_info));
  124. atomic_set(&tmp->use, 1);
  125. }
  126. nf_bridge_put(nf_bridge);
  127. nf_bridge = tmp;
  128. }
  129. return nf_bridge;
  130. }
  131. static inline void nf_bridge_push_encap_header(struct sk_buff *skb)
  132. {
  133. unsigned int len = nf_bridge_encap_header_len(skb);
  134. skb_push(skb, len);
  135. skb->network_header -= len;
  136. }
  137. static inline void nf_bridge_pull_encap_header(struct sk_buff *skb)
  138. {
  139. unsigned int len = nf_bridge_encap_header_len(skb);
  140. skb_pull(skb, len);
  141. skb->network_header += len;
  142. }
  143. static inline void nf_bridge_pull_encap_header_rcsum(struct sk_buff *skb)
  144. {
  145. unsigned int len = nf_bridge_encap_header_len(skb);
  146. skb_pull_rcsum(skb, len);
  147. skb->network_header += len;
  148. }
  149. static inline void nf_bridge_save_header(struct sk_buff *skb)
  150. {
  151. int header_size = ETH_HLEN + nf_bridge_encap_header_len(skb);
  152. skb_copy_from_linear_data_offset(skb, -header_size,
  153. skb->nf_bridge->data, header_size);
  154. }
  155. /* When handing a packet over to the IP layer
  156. * check whether we have a skb that is in the
  157. * expected format
  158. */
  159. static int br_parse_ip_options(struct sk_buff *skb)
  160. {
  161. const struct iphdr *iph;
  162. struct net_device *dev = skb->dev;
  163. u32 len;
  164. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  165. goto inhdr_error;
  166. iph = ip_hdr(skb);
  167. /* Basic sanity checks */
  168. if (iph->ihl < 5 || iph->version != 4)
  169. goto inhdr_error;
  170. if (!pskb_may_pull(skb, iph->ihl*4))
  171. goto inhdr_error;
  172. iph = ip_hdr(skb);
  173. if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
  174. goto inhdr_error;
  175. len = ntohs(iph->tot_len);
  176. if (skb->len < len) {
  177. IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_INTRUNCATEDPKTS);
  178. goto drop;
  179. } else if (len < (iph->ihl*4))
  180. goto inhdr_error;
  181. if (pskb_trim_rcsum(skb, len)) {
  182. IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_INDISCARDS);
  183. goto drop;
  184. }
  185. memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
  186. /* We should really parse IP options here but until
  187. * somebody who actually uses IP options complains to
  188. * us we'll just silently ignore the options because
  189. * we're lazy!
  190. */
  191. return 0;
  192. inhdr_error:
  193. IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_INHDRERRORS);
  194. drop:
  195. return -1;
  196. }
  197. /* PF_BRIDGE/PRE_ROUTING *********************************************/
  198. /* Undo the changes made for ip6tables PREROUTING and continue the
  199. * bridge PRE_ROUTING hook. */
  200. static int br_nf_pre_routing_finish_ipv6(struct sk_buff *skb)
  201. {
  202. struct nf_bridge_info *nf_bridge = skb->nf_bridge;
  203. struct rtable *rt;
  204. if (nf_bridge->mask & BRNF_PKT_TYPE) {
  205. skb->pkt_type = PACKET_OTHERHOST;
  206. nf_bridge->mask ^= BRNF_PKT_TYPE;
  207. }
  208. nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
  209. rt = bridge_parent_rtable(nf_bridge->physindev);
  210. if (!rt) {
  211. kfree_skb(skb);
  212. return 0;
  213. }
  214. skb_dst_set_noref(skb, &rt->dst);
  215. skb->dev = nf_bridge->physindev;
  216. nf_bridge_update_protocol(skb);
  217. nf_bridge_push_encap_header(skb);
  218. NF_HOOK_THRESH(NFPROTO_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL,
  219. br_handle_frame_finish, 1);
  220. return 0;
  221. }
  222. /* Obtain the correct destination MAC address, while preserving the original
  223. * source MAC address. If we already know this address, we just copy it. If we
  224. * don't, we use the neighbour framework to find out. In both cases, we make
  225. * sure that br_handle_frame_finish() is called afterwards.
  226. */
  227. static int br_nf_pre_routing_finish_bridge(struct sk_buff *skb)
  228. {
  229. struct nf_bridge_info *nf_bridge = skb->nf_bridge;
  230. struct neighbour *neigh;
  231. struct dst_entry *dst;
  232. skb->dev = bridge_parent(skb->dev);
  233. if (!skb->dev)
  234. goto free_skb;
  235. dst = skb_dst(skb);
  236. neigh = dst_neigh_lookup_skb(dst, skb);
  237. if (neigh) {
  238. int ret;
  239. if (neigh->hh.hh_len) {
  240. neigh_hh_bridge(&neigh->hh, skb);
  241. skb->dev = nf_bridge->physindev;
  242. ret = br_handle_frame_finish(skb);
  243. } else {
  244. /* the neighbour function below overwrites the complete
  245. * MAC header, so we save the Ethernet source address and
  246. * protocol number.
  247. */
  248. skb_copy_from_linear_data_offset(skb,
  249. -(ETH_HLEN-ETH_ALEN),
  250. skb->nf_bridge->data,
  251. ETH_HLEN-ETH_ALEN);
  252. /* tell br_dev_xmit to continue with forwarding */
  253. nf_bridge->mask |= BRNF_BRIDGED_DNAT;
  254. /* FIXME Need to refragment */
  255. ret = neigh->output(neigh, skb);
  256. }
  257. neigh_release(neigh);
  258. return ret;
  259. }
  260. free_skb:
  261. kfree_skb(skb);
  262. return 0;
  263. }
  264. /* This requires some explaining. If DNAT has taken place,
  265. * we will need to fix up the destination Ethernet address.
  266. *
  267. * There are two cases to consider:
  268. * 1. The packet was DNAT'ed to a device in the same bridge
  269. * port group as it was received on. We can still bridge
  270. * the packet.
  271. * 2. The packet was DNAT'ed to a different device, either
  272. * a non-bridged device or another bridge port group.
  273. * The packet will need to be routed.
  274. *
  275. * The correct way of distinguishing between these two cases is to
  276. * call ip_route_input() and to look at skb->dst->dev, which is
  277. * changed to the destination device if ip_route_input() succeeds.
  278. *
  279. * Let's first consider the case that ip_route_input() succeeds:
  280. *
  281. * If the output device equals the logical bridge device the packet
  282. * came in on, we can consider this bridging. The corresponding MAC
  283. * address will be obtained in br_nf_pre_routing_finish_bridge.
  284. * Otherwise, the packet is considered to be routed and we just
  285. * change the destination MAC address so that the packet will
  286. * later be passed up to the IP stack to be routed. For a redirected
  287. * packet, ip_route_input() will give back the localhost as output device,
  288. * which differs from the bridge device.
  289. *
  290. * Let's now consider the case that ip_route_input() fails:
  291. *
  292. * This can be because the destination address is martian, in which case
  293. * the packet will be dropped.
  294. * If IP forwarding is disabled, ip_route_input() will fail, while
  295. * ip_route_output_key() can return success. The source
  296. * address for ip_route_output_key() is set to zero, so ip_route_output_key()
  297. * thinks we're handling a locally generated packet and won't care
  298. * if IP forwarding is enabled. If the output device equals the logical bridge
  299. * device, we proceed as if ip_route_input() succeeded. If it differs from the
  300. * logical bridge port or if ip_route_output_key() fails we drop the packet.
  301. */
  302. static int br_nf_pre_routing_finish(struct sk_buff *skb)
  303. {
  304. struct net_device *dev = skb->dev;
  305. struct iphdr *iph = ip_hdr(skb);
  306. struct nf_bridge_info *nf_bridge = skb->nf_bridge;
  307. struct rtable *rt;
  308. int err;
  309. int frag_max_size;
  310. frag_max_size = IPCB(skb)->frag_max_size;
  311. BR_INPUT_SKB_CB(skb)->frag_max_size = frag_max_size;
  312. if (nf_bridge->mask & BRNF_PKT_TYPE) {
  313. skb->pkt_type = PACKET_OTHERHOST;
  314. nf_bridge->mask ^= BRNF_PKT_TYPE;
  315. }
  316. nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
  317. if (dnat_took_place(skb)) {
  318. if ((err = ip_route_input(skb, iph->daddr, iph->saddr, iph->tos, dev))) {
  319. struct in_device *in_dev = __in_dev_get_rcu(dev);
  320. /* If err equals -EHOSTUNREACH the error is due to a
  321. * martian destination or due to the fact that
  322. * forwarding is disabled. For most martian packets,
  323. * ip_route_output_key() will fail. It won't fail for 2 types of
  324. * martian destinations: loopback destinations and destination
  325. * 0.0.0.0. In both cases the packet will be dropped because the
  326. * destination is the loopback device and not the bridge. */
  327. if (err != -EHOSTUNREACH || !in_dev || IN_DEV_FORWARD(in_dev))
  328. goto free_skb;
  329. rt = ip_route_output(dev_net(dev), iph->daddr, 0,
  330. RT_TOS(iph->tos), 0);
  331. if (!IS_ERR(rt)) {
  332. /* - Bridged-and-DNAT'ed traffic doesn't
  333. * require ip_forwarding. */
  334. if (rt->dst.dev == dev) {
  335. skb_dst_set(skb, &rt->dst);
  336. goto bridged_dnat;
  337. }
  338. ip_rt_put(rt);
  339. }
  340. free_skb:
  341. kfree_skb(skb);
  342. return 0;
  343. } else {
  344. if (skb_dst(skb)->dev == dev) {
  345. bridged_dnat:
  346. skb->dev = nf_bridge->physindev;
  347. nf_bridge_update_protocol(skb);
  348. nf_bridge_push_encap_header(skb);
  349. NF_HOOK_THRESH(NFPROTO_BRIDGE,
  350. NF_BR_PRE_ROUTING,
  351. skb, skb->dev, NULL,
  352. br_nf_pre_routing_finish_bridge,
  353. 1);
  354. return 0;
  355. }
  356. ether_addr_copy(eth_hdr(skb)->h_dest, dev->dev_addr);
  357. skb->pkt_type = PACKET_HOST;
  358. }
  359. } else {
  360. rt = bridge_parent_rtable(nf_bridge->physindev);
  361. if (!rt) {
  362. kfree_skb(skb);
  363. return 0;
  364. }
  365. skb_dst_set_noref(skb, &rt->dst);
  366. }
  367. skb->dev = nf_bridge->physindev;
  368. nf_bridge_update_protocol(skb);
  369. nf_bridge_push_encap_header(skb);
  370. NF_HOOK_THRESH(NFPROTO_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL,
  371. br_handle_frame_finish, 1);
  372. return 0;
  373. }
  374. static struct net_device *brnf_get_logical_dev(struct sk_buff *skb, const struct net_device *dev)
  375. {
  376. struct net_device *vlan, *br;
  377. br = bridge_parent(dev);
  378. if (brnf_pass_vlan_indev == 0 || !vlan_tx_tag_present(skb))
  379. return br;
  380. vlan = __vlan_find_dev_deep_rcu(br, skb->vlan_proto,
  381. vlan_tx_tag_get(skb) & VLAN_VID_MASK);
  382. return vlan ? vlan : br;
  383. }
  384. /* Some common code for IPv4/IPv6 */
  385. static struct net_device *setup_pre_routing(struct sk_buff *skb)
  386. {
  387. struct nf_bridge_info *nf_bridge = skb->nf_bridge;
  388. if (skb->pkt_type == PACKET_OTHERHOST) {
  389. skb->pkt_type = PACKET_HOST;
  390. nf_bridge->mask |= BRNF_PKT_TYPE;
  391. }
  392. nf_bridge->mask |= BRNF_NF_BRIDGE_PREROUTING;
  393. nf_bridge->physindev = skb->dev;
  394. skb->dev = brnf_get_logical_dev(skb, skb->dev);
  395. if (skb->protocol == htons(ETH_P_8021Q))
  396. nf_bridge->mask |= BRNF_8021Q;
  397. else if (skb->protocol == htons(ETH_P_PPP_SES))
  398. nf_bridge->mask |= BRNF_PPPoE;
  399. /* Must drop socket now because of tproxy. */
  400. skb_orphan(skb);
  401. return skb->dev;
  402. }
  403. /* We only check the length. A bridge shouldn't do any hop-by-hop stuff anyway */
  404. static int check_hbh_len(struct sk_buff *skb)
  405. {
  406. unsigned char *raw = (u8 *)(ipv6_hdr(skb) + 1);
  407. u32 pkt_len;
  408. const unsigned char *nh = skb_network_header(skb);
  409. int off = raw - nh;
  410. int len = (raw[1] + 1) << 3;
  411. if ((raw + len) - skb->data > skb_headlen(skb))
  412. goto bad;
  413. off += 2;
  414. len -= 2;
  415. while (len > 0) {
  416. int optlen = nh[off + 1] + 2;
  417. switch (nh[off]) {
  418. case IPV6_TLV_PAD1:
  419. optlen = 1;
  420. break;
  421. case IPV6_TLV_PADN:
  422. break;
  423. case IPV6_TLV_JUMBO:
  424. if (nh[off + 1] != 4 || (off & 3) != 2)
  425. goto bad;
  426. pkt_len = ntohl(*(__be32 *) (nh + off + 2));
  427. if (pkt_len <= IPV6_MAXPLEN ||
  428. ipv6_hdr(skb)->payload_len)
  429. goto bad;
  430. if (pkt_len > skb->len - sizeof(struct ipv6hdr))
  431. goto bad;
  432. if (pskb_trim_rcsum(skb,
  433. pkt_len + sizeof(struct ipv6hdr)))
  434. goto bad;
  435. nh = skb_network_header(skb);
  436. break;
  437. default:
  438. if (optlen > len)
  439. goto bad;
  440. break;
  441. }
  442. off += optlen;
  443. len -= optlen;
  444. }
  445. if (len == 0)
  446. return 0;
  447. bad:
  448. return -1;
  449. }
  450. /* Replicate the checks that IPv6 does on packet reception and pass the packet
  451. * to ip6tables, which doesn't support NAT, so things are fairly simple. */
  452. static unsigned int br_nf_pre_routing_ipv6(const struct nf_hook_ops *ops,
  453. struct sk_buff *skb,
  454. const struct net_device *in,
  455. const struct net_device *out,
  456. int (*okfn)(struct sk_buff *))
  457. {
  458. const struct ipv6hdr *hdr;
  459. u32 pkt_len;
  460. if (skb->len < sizeof(struct ipv6hdr))
  461. return NF_DROP;
  462. if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
  463. return NF_DROP;
  464. hdr = ipv6_hdr(skb);
  465. if (hdr->version != 6)
  466. return NF_DROP;
  467. pkt_len = ntohs(hdr->payload_len);
  468. if (pkt_len || hdr->nexthdr != NEXTHDR_HOP) {
  469. if (pkt_len + sizeof(struct ipv6hdr) > skb->len)
  470. return NF_DROP;
  471. if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr)))
  472. return NF_DROP;
  473. }
  474. if (hdr->nexthdr == NEXTHDR_HOP && check_hbh_len(skb))
  475. return NF_DROP;
  476. nf_bridge_put(skb->nf_bridge);
  477. if (!nf_bridge_alloc(skb))
  478. return NF_DROP;
  479. if (!setup_pre_routing(skb))
  480. return NF_DROP;
  481. skb->protocol = htons(ETH_P_IPV6);
  482. NF_HOOK(NFPROTO_IPV6, NF_INET_PRE_ROUTING, skb, skb->dev, NULL,
  483. br_nf_pre_routing_finish_ipv6);
  484. return NF_STOLEN;
  485. }
  486. /* Direct IPv6 traffic to br_nf_pre_routing_ipv6.
  487. * Replicate the checks that IPv4 does on packet reception.
  488. * Set skb->dev to the bridge device (i.e. parent of the
  489. * receiving device) to make netfilter happy, the REDIRECT
  490. * target in particular. Save the original destination IP
  491. * address to be able to detect DNAT afterwards. */
  492. static unsigned int br_nf_pre_routing(const struct nf_hook_ops *ops,
  493. struct sk_buff *skb,
  494. const struct net_device *in,
  495. const struct net_device *out,
  496. int (*okfn)(struct sk_buff *))
  497. {
  498. struct net_bridge_port *p;
  499. struct net_bridge *br;
  500. __u32 len = nf_bridge_encap_header_len(skb);
  501. if (unlikely(!pskb_may_pull(skb, len)))
  502. return NF_DROP;
  503. p = br_port_get_rcu(in);
  504. if (p == NULL)
  505. return NF_DROP;
  506. br = p->br;
  507. if (IS_IPV6(skb) || IS_VLAN_IPV6(skb) || IS_PPPOE_IPV6(skb)) {
  508. if (!brnf_call_ip6tables && !br->nf_call_ip6tables)
  509. return NF_ACCEPT;
  510. nf_bridge_pull_encap_header_rcsum(skb);
  511. return br_nf_pre_routing_ipv6(ops, skb, in, out, okfn);
  512. }
  513. if (!brnf_call_iptables && !br->nf_call_iptables)
  514. return NF_ACCEPT;
  515. if (!IS_IP(skb) && !IS_VLAN_IP(skb) && !IS_PPPOE_IP(skb))
  516. return NF_ACCEPT;
  517. nf_bridge_pull_encap_header_rcsum(skb);
  518. if (br_parse_ip_options(skb))
  519. return NF_DROP;
  520. nf_bridge_put(skb->nf_bridge);
  521. if (!nf_bridge_alloc(skb))
  522. return NF_DROP;
  523. if (!setup_pre_routing(skb))
  524. return NF_DROP;
  525. store_orig_dstaddr(skb);
  526. skb->protocol = htons(ETH_P_IP);
  527. NF_HOOK(NFPROTO_IPV4, NF_INET_PRE_ROUTING, skb, skb->dev, NULL,
  528. br_nf_pre_routing_finish);
  529. return NF_STOLEN;
  530. }
  531. /* PF_BRIDGE/LOCAL_IN ************************************************/
  532. /* The packet is locally destined, which requires a real
  533. * dst_entry, so detach the fake one. On the way up, the
  534. * packet would pass through PRE_ROUTING again (which already
  535. * took place when the packet entered the bridge), but we
  536. * register an IPv4 PRE_ROUTING 'sabotage' hook that will
  537. * prevent this from happening. */
  538. static unsigned int br_nf_local_in(const struct nf_hook_ops *ops,
  539. struct sk_buff *skb,
  540. const struct net_device *in,
  541. const struct net_device *out,
  542. int (*okfn)(struct sk_buff *))
  543. {
  544. br_drop_fake_rtable(skb);
  545. return NF_ACCEPT;
  546. }
  547. /* PF_BRIDGE/FORWARD *************************************************/
  548. static int br_nf_forward_finish(struct sk_buff *skb)
  549. {
  550. struct nf_bridge_info *nf_bridge = skb->nf_bridge;
  551. struct net_device *in;
  552. if (!IS_ARP(skb) && !IS_VLAN_ARP(skb)) {
  553. int frag_max_size;
  554. if (skb->protocol == htons(ETH_P_IP)) {
  555. frag_max_size = IPCB(skb)->frag_max_size;
  556. BR_INPUT_SKB_CB(skb)->frag_max_size = frag_max_size;
  557. }
  558. in = nf_bridge->physindev;
  559. if (nf_bridge->mask & BRNF_PKT_TYPE) {
  560. skb->pkt_type = PACKET_OTHERHOST;
  561. nf_bridge->mask ^= BRNF_PKT_TYPE;
  562. }
  563. nf_bridge_update_protocol(skb);
  564. } else {
  565. in = *((struct net_device **)(skb->cb));
  566. }
  567. nf_bridge_push_encap_header(skb);
  568. NF_HOOK_THRESH(NFPROTO_BRIDGE, NF_BR_FORWARD, skb, in,
  569. skb->dev, br_forward_finish, 1);
  570. return 0;
  571. }
  572. /* This is the 'purely bridged' case. For IP, we pass the packet to
  573. * netfilter with indev and outdev set to the bridge device,
  574. * but we are still able to filter on the 'real' indev/outdev
  575. * because of the physdev module. For ARP, indev and outdev are the
  576. * bridge ports. */
  577. static unsigned int br_nf_forward_ip(const struct nf_hook_ops *ops,
  578. struct sk_buff *skb,
  579. const struct net_device *in,
  580. const struct net_device *out,
  581. int (*okfn)(struct sk_buff *))
  582. {
  583. struct nf_bridge_info *nf_bridge;
  584. struct net_device *parent;
  585. u_int8_t pf;
  586. if (!skb->nf_bridge)
  587. return NF_ACCEPT;
  588. /* Need exclusive nf_bridge_info since we might have multiple
  589. * different physoutdevs. */
  590. if (!nf_bridge_unshare(skb))
  591. return NF_DROP;
  592. parent = bridge_parent(out);
  593. if (!parent)
  594. return NF_DROP;
  595. if (IS_IP(skb) || IS_VLAN_IP(skb) || IS_PPPOE_IP(skb))
  596. pf = NFPROTO_IPV4;
  597. else if (IS_IPV6(skb) || IS_VLAN_IPV6(skb) || IS_PPPOE_IPV6(skb))
  598. pf = NFPROTO_IPV6;
  599. else
  600. return NF_ACCEPT;
  601. nf_bridge_pull_encap_header(skb);
  602. nf_bridge = skb->nf_bridge;
  603. if (skb->pkt_type == PACKET_OTHERHOST) {
  604. skb->pkt_type = PACKET_HOST;
  605. nf_bridge->mask |= BRNF_PKT_TYPE;
  606. }
  607. if (pf == NFPROTO_IPV4) {
  608. int frag_max = BR_INPUT_SKB_CB(skb)->frag_max_size;
  609. if (br_parse_ip_options(skb))
  610. return NF_DROP;
  611. IPCB(skb)->frag_max_size = frag_max;
  612. }
  613. /* The physdev module checks on this */
  614. nf_bridge->mask |= BRNF_BRIDGED;
  615. nf_bridge->physoutdev = skb->dev;
  616. if (pf == NFPROTO_IPV4)
  617. skb->protocol = htons(ETH_P_IP);
  618. else
  619. skb->protocol = htons(ETH_P_IPV6);
  620. NF_HOOK(pf, NF_INET_FORWARD, skb, brnf_get_logical_dev(skb, in), parent,
  621. br_nf_forward_finish);
  622. return NF_STOLEN;
  623. }
  624. static unsigned int br_nf_forward_arp(const struct nf_hook_ops *ops,
  625. struct sk_buff *skb,
  626. const struct net_device *in,
  627. const struct net_device *out,
  628. int (*okfn)(struct sk_buff *))
  629. {
  630. struct net_bridge_port *p;
  631. struct net_bridge *br;
  632. struct net_device **d = (struct net_device **)(skb->cb);
  633. p = br_port_get_rcu(out);
  634. if (p == NULL)
  635. return NF_ACCEPT;
  636. br = p->br;
  637. if (!brnf_call_arptables && !br->nf_call_arptables)
  638. return NF_ACCEPT;
  639. if (!IS_ARP(skb)) {
  640. if (!IS_VLAN_ARP(skb))
  641. return NF_ACCEPT;
  642. nf_bridge_pull_encap_header(skb);
  643. }
  644. if (arp_hdr(skb)->ar_pln != 4) {
  645. if (IS_VLAN_ARP(skb))
  646. nf_bridge_push_encap_header(skb);
  647. return NF_ACCEPT;
  648. }
  649. *d = (struct net_device *)in;
  650. NF_HOOK(NFPROTO_ARP, NF_ARP_FORWARD, skb, (struct net_device *)in,
  651. (struct net_device *)out, br_nf_forward_finish);
  652. return NF_STOLEN;
  653. }
  654. #if IS_ENABLED(CONFIG_NF_DEFRAG_IPV4)
  655. static int br_nf_dev_queue_xmit(struct sk_buff *skb)
  656. {
  657. int ret;
  658. int frag_max_size;
  659. /* This is wrong! We should preserve the original fragment
  660. * boundaries by preserving frag_list rather than refragmenting.
  661. */
  662. if (skb->protocol == htons(ETH_P_IP) &&
  663. skb->len + nf_bridge_mtu_reduction(skb) > skb->dev->mtu &&
  664. !skb_is_gso(skb)) {
  665. frag_max_size = BR_INPUT_SKB_CB(skb)->frag_max_size;
  666. if (br_parse_ip_options(skb))
  667. /* Drop invalid packet */
  668. return NF_DROP;
  669. IPCB(skb)->frag_max_size = frag_max_size;
  670. ret = ip_fragment(skb, br_dev_queue_push_xmit);
  671. } else
  672. ret = br_dev_queue_push_xmit(skb);
  673. return ret;
  674. }
  675. #else
  676. static int br_nf_dev_queue_xmit(struct sk_buff *skb)
  677. {
  678. return br_dev_queue_push_xmit(skb);
  679. }
  680. #endif
  681. /* PF_BRIDGE/POST_ROUTING ********************************************/
  682. static unsigned int br_nf_post_routing(const struct nf_hook_ops *ops,
  683. struct sk_buff *skb,
  684. const struct net_device *in,
  685. const struct net_device *out,
  686. int (*okfn)(struct sk_buff *))
  687. {
  688. struct nf_bridge_info *nf_bridge = skb->nf_bridge;
  689. struct net_device *realoutdev = bridge_parent(skb->dev);
  690. u_int8_t pf;
  691. if (!nf_bridge || !(nf_bridge->mask & BRNF_BRIDGED))
  692. return NF_ACCEPT;
  693. if (!realoutdev)
  694. return NF_DROP;
  695. if (IS_IP(skb) || IS_VLAN_IP(skb) || IS_PPPOE_IP(skb))
  696. pf = NFPROTO_IPV4;
  697. else if (IS_IPV6(skb) || IS_VLAN_IPV6(skb) || IS_PPPOE_IPV6(skb))
  698. pf = NFPROTO_IPV6;
  699. else
  700. return NF_ACCEPT;
  701. /* We assume any code from br_dev_queue_push_xmit onwards doesn't care
  702. * about the value of skb->pkt_type. */
  703. if (skb->pkt_type == PACKET_OTHERHOST) {
  704. skb->pkt_type = PACKET_HOST;
  705. nf_bridge->mask |= BRNF_PKT_TYPE;
  706. }
  707. nf_bridge_pull_encap_header(skb);
  708. nf_bridge_save_header(skb);
  709. if (pf == NFPROTO_IPV4)
  710. skb->protocol = htons(ETH_P_IP);
  711. else
  712. skb->protocol = htons(ETH_P_IPV6);
  713. NF_HOOK(pf, NF_INET_POST_ROUTING, skb, NULL, realoutdev,
  714. br_nf_dev_queue_xmit);
  715. return NF_STOLEN;
  716. }
  717. /* IP/SABOTAGE *****************************************************/
  718. /* Don't hand locally destined packets to PF_INET(6)/PRE_ROUTING
  719. * for the second time. */
  720. static unsigned int ip_sabotage_in(const struct nf_hook_ops *ops,
  721. struct sk_buff *skb,
  722. const struct net_device *in,
  723. const struct net_device *out,
  724. int (*okfn)(struct sk_buff *))
  725. {
  726. if (skb->nf_bridge &&
  727. !(skb->nf_bridge->mask & BRNF_NF_BRIDGE_PREROUTING)) {
  728. return NF_STOP;
  729. }
  730. return NF_ACCEPT;
  731. }
  732. void br_netfilter_enable(void)
  733. {
  734. }
  735. EXPORT_SYMBOL_GPL(br_netfilter_enable);
  736. /* For br_nf_post_routing, we need (prio = NF_BR_PRI_LAST), because
  737. * br_dev_queue_push_xmit is called afterwards */
  738. static struct nf_hook_ops br_nf_ops[] __read_mostly = {
  739. {
  740. .hook = br_nf_pre_routing,
  741. .owner = THIS_MODULE,
  742. .pf = NFPROTO_BRIDGE,
  743. .hooknum = NF_BR_PRE_ROUTING,
  744. .priority = NF_BR_PRI_BRNF,
  745. },
  746. {
  747. .hook = br_nf_local_in,
  748. .owner = THIS_MODULE,
  749. .pf = NFPROTO_BRIDGE,
  750. .hooknum = NF_BR_LOCAL_IN,
  751. .priority = NF_BR_PRI_BRNF,
  752. },
  753. {
  754. .hook = br_nf_forward_ip,
  755. .owner = THIS_MODULE,
  756. .pf = NFPROTO_BRIDGE,
  757. .hooknum = NF_BR_FORWARD,
  758. .priority = NF_BR_PRI_BRNF - 1,
  759. },
  760. {
  761. .hook = br_nf_forward_arp,
  762. .owner = THIS_MODULE,
  763. .pf = NFPROTO_BRIDGE,
  764. .hooknum = NF_BR_FORWARD,
  765. .priority = NF_BR_PRI_BRNF,
  766. },
  767. {
  768. .hook = br_nf_post_routing,
  769. .owner = THIS_MODULE,
  770. .pf = NFPROTO_BRIDGE,
  771. .hooknum = NF_BR_POST_ROUTING,
  772. .priority = NF_BR_PRI_LAST,
  773. },
  774. {
  775. .hook = ip_sabotage_in,
  776. .owner = THIS_MODULE,
  777. .pf = NFPROTO_IPV4,
  778. .hooknum = NF_INET_PRE_ROUTING,
  779. .priority = NF_IP_PRI_FIRST,
  780. },
  781. {
  782. .hook = ip_sabotage_in,
  783. .owner = THIS_MODULE,
  784. .pf = NFPROTO_IPV6,
  785. .hooknum = NF_INET_PRE_ROUTING,
  786. .priority = NF_IP6_PRI_FIRST,
  787. },
  788. };
  789. #ifdef CONFIG_SYSCTL
  790. static
  791. int brnf_sysctl_call_tables(struct ctl_table *ctl, int write,
  792. void __user *buffer, size_t *lenp, loff_t *ppos)
  793. {
  794. int ret;
  795. ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
  796. if (write && *(int *)(ctl->data))
  797. *(int *)(ctl->data) = 1;
  798. return ret;
  799. }
  800. static struct ctl_table brnf_table[] = {
  801. {
  802. .procname = "bridge-nf-call-arptables",
  803. .data = &brnf_call_arptables,
  804. .maxlen = sizeof(int),
  805. .mode = 0644,
  806. .proc_handler = brnf_sysctl_call_tables,
  807. },
  808. {
  809. .procname = "bridge-nf-call-iptables",
  810. .data = &brnf_call_iptables,
  811. .maxlen = sizeof(int),
  812. .mode = 0644,
  813. .proc_handler = brnf_sysctl_call_tables,
  814. },
  815. {
  816. .procname = "bridge-nf-call-ip6tables",
  817. .data = &brnf_call_ip6tables,
  818. .maxlen = sizeof(int),
  819. .mode = 0644,
  820. .proc_handler = brnf_sysctl_call_tables,
  821. },
  822. {
  823. .procname = "bridge-nf-filter-vlan-tagged",
  824. .data = &brnf_filter_vlan_tagged,
  825. .maxlen = sizeof(int),
  826. .mode = 0644,
  827. .proc_handler = brnf_sysctl_call_tables,
  828. },
  829. {
  830. .procname = "bridge-nf-filter-pppoe-tagged",
  831. .data = &brnf_filter_pppoe_tagged,
  832. .maxlen = sizeof(int),
  833. .mode = 0644,
  834. .proc_handler = brnf_sysctl_call_tables,
  835. },
  836. {
  837. .procname = "bridge-nf-pass-vlan-input-dev",
  838. .data = &brnf_pass_vlan_indev,
  839. .maxlen = sizeof(int),
  840. .mode = 0644,
  841. .proc_handler = brnf_sysctl_call_tables,
  842. },
  843. { }
  844. };
  845. #endif
  846. static int __init br_netfilter_init(void)
  847. {
  848. int ret;
  849. ret = nf_register_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
  850. if (ret < 0)
  851. return ret;
  852. #ifdef CONFIG_SYSCTL
  853. brnf_sysctl_header = register_net_sysctl(&init_net, "net/bridge", brnf_table);
  854. if (brnf_sysctl_header == NULL) {
  855. printk(KERN_WARNING
  856. "br_netfilter: can't register to sysctl.\n");
  857. ret = -ENOMEM;
  858. goto err1;
  859. }
  860. #endif
  861. printk(KERN_NOTICE "Bridge firewalling registered\n");
  862. return 0;
  863. err1:
  864. nf_unregister_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
  865. return ret;
  866. }
  867. static void __exit br_netfilter_fini(void)
  868. {
  869. nf_unregister_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
  870. #ifdef CONFIG_SYSCTL
  871. unregister_net_sysctl_table(brnf_sysctl_header);
  872. #endif
  873. }
  874. module_init(br_netfilter_init);
  875. module_exit(br_netfilter_fini);
  876. MODULE_LICENSE("GPL");
  877. MODULE_AUTHOR("Lennert Buytenhek <buytenh@gnu.org>");
  878. MODULE_AUTHOR("Bart De Schuymer <bdschuym@pandora.be>");
  879. MODULE_DESCRIPTION("Linux ethernet netfilter firewall bridge");