br_netfilter_hooks.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015
  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/addrconf.h>
  36. #include <net/route.h>
  37. #include <net/netfilter/br_netfilter.h>
  38. #include <asm/uaccess.h>
  39. #include "br_private.h"
  40. #ifdef CONFIG_SYSCTL
  41. #include <linux/sysctl.h>
  42. #endif
  43. #ifdef CONFIG_SYSCTL
  44. static struct ctl_table_header *brnf_sysctl_header;
  45. static int brnf_call_iptables __read_mostly = 1;
  46. static int brnf_call_ip6tables __read_mostly = 1;
  47. static int brnf_call_arptables __read_mostly = 1;
  48. static int brnf_filter_vlan_tagged __read_mostly;
  49. static int brnf_filter_pppoe_tagged __read_mostly;
  50. static int brnf_pass_vlan_indev __read_mostly;
  51. #else
  52. #define brnf_call_iptables 1
  53. #define brnf_call_ip6tables 1
  54. #define brnf_call_arptables 1
  55. #define brnf_filter_vlan_tagged 0
  56. #define brnf_filter_pppoe_tagged 0
  57. #define brnf_pass_vlan_indev 0
  58. #endif
  59. #define IS_IP(skb) \
  60. (!skb_vlan_tag_present(skb) && skb->protocol == htons(ETH_P_IP))
  61. #define IS_IPV6(skb) \
  62. (!skb_vlan_tag_present(skb) && skb->protocol == htons(ETH_P_IPV6))
  63. #define IS_ARP(skb) \
  64. (!skb_vlan_tag_present(skb) && skb->protocol == htons(ETH_P_ARP))
  65. static inline __be16 vlan_proto(const struct sk_buff *skb)
  66. {
  67. if (skb_vlan_tag_present(skb))
  68. return skb->protocol;
  69. else if (skb->protocol == htons(ETH_P_8021Q))
  70. return vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
  71. else
  72. return 0;
  73. }
  74. #define IS_VLAN_IP(skb) \
  75. (vlan_proto(skb) == htons(ETH_P_IP) && \
  76. brnf_filter_vlan_tagged)
  77. #define IS_VLAN_IPV6(skb) \
  78. (vlan_proto(skb) == htons(ETH_P_IPV6) && \
  79. brnf_filter_vlan_tagged)
  80. #define IS_VLAN_ARP(skb) \
  81. (vlan_proto(skb) == htons(ETH_P_ARP) && \
  82. brnf_filter_vlan_tagged)
  83. static inline __be16 pppoe_proto(const struct sk_buff *skb)
  84. {
  85. return *((__be16 *)(skb_mac_header(skb) + ETH_HLEN +
  86. sizeof(struct pppoe_hdr)));
  87. }
  88. #define IS_PPPOE_IP(skb) \
  89. (skb->protocol == htons(ETH_P_PPP_SES) && \
  90. pppoe_proto(skb) == htons(PPP_IP) && \
  91. brnf_filter_pppoe_tagged)
  92. #define IS_PPPOE_IPV6(skb) \
  93. (skb->protocol == htons(ETH_P_PPP_SES) && \
  94. pppoe_proto(skb) == htons(PPP_IPV6) && \
  95. brnf_filter_pppoe_tagged)
  96. /* largest possible L2 header, see br_nf_dev_queue_xmit() */
  97. #define NF_BRIDGE_MAX_MAC_HEADER_LENGTH (PPPOE_SES_HLEN + ETH_HLEN)
  98. struct brnf_frag_data {
  99. char mac[NF_BRIDGE_MAX_MAC_HEADER_LENGTH];
  100. u8 encap_size;
  101. u8 size;
  102. u16 vlan_tci;
  103. __be16 vlan_proto;
  104. };
  105. static DEFINE_PER_CPU(struct brnf_frag_data, brnf_frag_data_storage);
  106. static void nf_bridge_info_free(struct sk_buff *skb)
  107. {
  108. if (skb->nf_bridge) {
  109. nf_bridge_put(skb->nf_bridge);
  110. skb->nf_bridge = NULL;
  111. }
  112. }
  113. static inline struct net_device *bridge_parent(const struct net_device *dev)
  114. {
  115. struct net_bridge_port *port;
  116. port = br_port_get_rcu(dev);
  117. return port ? port->br->dev : NULL;
  118. }
  119. static inline struct nf_bridge_info *nf_bridge_unshare(struct sk_buff *skb)
  120. {
  121. struct nf_bridge_info *nf_bridge = skb->nf_bridge;
  122. if (atomic_read(&nf_bridge->use) > 1) {
  123. struct nf_bridge_info *tmp = nf_bridge_alloc(skb);
  124. if (tmp) {
  125. memcpy(tmp, nf_bridge, sizeof(struct nf_bridge_info));
  126. atomic_set(&tmp->use, 1);
  127. }
  128. nf_bridge_put(nf_bridge);
  129. nf_bridge = tmp;
  130. }
  131. return nf_bridge;
  132. }
  133. unsigned int nf_bridge_encap_header_len(const struct sk_buff *skb)
  134. {
  135. switch (skb->protocol) {
  136. case __cpu_to_be16(ETH_P_8021Q):
  137. return VLAN_HLEN;
  138. case __cpu_to_be16(ETH_P_PPP_SES):
  139. return PPPOE_SES_HLEN;
  140. default:
  141. return 0;
  142. }
  143. }
  144. static inline void nf_bridge_pull_encap_header(struct sk_buff *skb)
  145. {
  146. unsigned int len = nf_bridge_encap_header_len(skb);
  147. skb_pull(skb, len);
  148. skb->network_header += len;
  149. }
  150. static inline void nf_bridge_pull_encap_header_rcsum(struct sk_buff *skb)
  151. {
  152. unsigned int len = nf_bridge_encap_header_len(skb);
  153. skb_pull_rcsum(skb, len);
  154. skb->network_header += len;
  155. }
  156. /* When handing a packet over to the IP layer
  157. * check whether we have a skb that is in the
  158. * expected format
  159. */
  160. static int br_validate_ipv4(struct net *net, struct sk_buff *skb)
  161. {
  162. const struct iphdr *iph;
  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(net, 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(net, 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(net, IPSTATS_MIB_INHDRERRORS);
  194. drop:
  195. return -1;
  196. }
  197. void nf_bridge_update_protocol(struct sk_buff *skb)
  198. {
  199. switch (skb->nf_bridge->orig_proto) {
  200. case BRNF_PROTO_8021Q:
  201. skb->protocol = htons(ETH_P_8021Q);
  202. break;
  203. case BRNF_PROTO_PPPOE:
  204. skb->protocol = htons(ETH_P_PPP_SES);
  205. break;
  206. case BRNF_PROTO_UNCHANGED:
  207. break;
  208. }
  209. }
  210. /* Obtain the correct destination MAC address, while preserving the original
  211. * source MAC address. If we already know this address, we just copy it. If we
  212. * don't, we use the neighbour framework to find out. In both cases, we make
  213. * sure that br_handle_frame_finish() is called afterwards.
  214. */
  215. int br_nf_pre_routing_finish_bridge(struct net *net, struct sock *sk, struct sk_buff *skb)
  216. {
  217. struct neighbour *neigh;
  218. struct dst_entry *dst;
  219. skb->dev = bridge_parent(skb->dev);
  220. if (!skb->dev)
  221. goto free_skb;
  222. dst = skb_dst(skb);
  223. neigh = dst_neigh_lookup_skb(dst, skb);
  224. if (neigh) {
  225. struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
  226. int ret;
  227. if ((neigh->nud_state & NUD_CONNECTED) && neigh->hh.hh_len) {
  228. neigh_hh_bridge(&neigh->hh, skb);
  229. skb->dev = nf_bridge->physindev;
  230. ret = br_handle_frame_finish(net, sk, skb);
  231. } else {
  232. /* the neighbour function below overwrites the complete
  233. * MAC header, so we save the Ethernet source address and
  234. * protocol number.
  235. */
  236. skb_copy_from_linear_data_offset(skb,
  237. -(ETH_HLEN-ETH_ALEN),
  238. nf_bridge->neigh_header,
  239. ETH_HLEN-ETH_ALEN);
  240. /* tell br_dev_xmit to continue with forwarding */
  241. nf_bridge->bridged_dnat = 1;
  242. /* FIXME Need to refragment */
  243. ret = neigh->output(neigh, skb);
  244. }
  245. neigh_release(neigh);
  246. return ret;
  247. }
  248. free_skb:
  249. kfree_skb(skb);
  250. return 0;
  251. }
  252. static inline bool
  253. br_nf_ipv4_daddr_was_changed(const struct sk_buff *skb,
  254. const struct nf_bridge_info *nf_bridge)
  255. {
  256. return ip_hdr(skb)->daddr != nf_bridge->ipv4_daddr;
  257. }
  258. /* This requires some explaining. If DNAT has taken place,
  259. * we will need to fix up the destination Ethernet address.
  260. * This is also true when SNAT takes place (for the reply direction).
  261. *
  262. * There are two cases to consider:
  263. * 1. The packet was DNAT'ed to a device in the same bridge
  264. * port group as it was received on. We can still bridge
  265. * the packet.
  266. * 2. The packet was DNAT'ed to a different device, either
  267. * a non-bridged device or another bridge port group.
  268. * The packet will need to be routed.
  269. *
  270. * The correct way of distinguishing between these two cases is to
  271. * call ip_route_input() and to look at skb->dst->dev, which is
  272. * changed to the destination device if ip_route_input() succeeds.
  273. *
  274. * Let's first consider the case that ip_route_input() succeeds:
  275. *
  276. * If the output device equals the logical bridge device the packet
  277. * came in on, we can consider this bridging. The corresponding MAC
  278. * address will be obtained in br_nf_pre_routing_finish_bridge.
  279. * Otherwise, the packet is considered to be routed and we just
  280. * change the destination MAC address so that the packet will
  281. * later be passed up to the IP stack to be routed. For a redirected
  282. * packet, ip_route_input() will give back the localhost as output device,
  283. * which differs from the bridge device.
  284. *
  285. * Let's now consider the case that ip_route_input() fails:
  286. *
  287. * This can be because the destination address is martian, in which case
  288. * the packet will be dropped.
  289. * If IP forwarding is disabled, ip_route_input() will fail, while
  290. * ip_route_output_key() can return success. The source
  291. * address for ip_route_output_key() is set to zero, so ip_route_output_key()
  292. * thinks we're handling a locally generated packet and won't care
  293. * if IP forwarding is enabled. If the output device equals the logical bridge
  294. * device, we proceed as if ip_route_input() succeeded. If it differs from the
  295. * logical bridge port or if ip_route_output_key() fails we drop the packet.
  296. */
  297. static int br_nf_pre_routing_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
  298. {
  299. struct net_device *dev = skb->dev;
  300. struct iphdr *iph = ip_hdr(skb);
  301. struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
  302. struct rtable *rt;
  303. int err;
  304. nf_bridge->frag_max_size = IPCB(skb)->frag_max_size;
  305. if (nf_bridge->pkt_otherhost) {
  306. skb->pkt_type = PACKET_OTHERHOST;
  307. nf_bridge->pkt_otherhost = false;
  308. }
  309. nf_bridge->in_prerouting = 0;
  310. if (br_nf_ipv4_daddr_was_changed(skb, nf_bridge)) {
  311. if ((err = ip_route_input(skb, iph->daddr, iph->saddr, iph->tos, dev))) {
  312. struct in_device *in_dev = __in_dev_get_rcu(dev);
  313. /* If err equals -EHOSTUNREACH the error is due to a
  314. * martian destination or due to the fact that
  315. * forwarding is disabled. For most martian packets,
  316. * ip_route_output_key() will fail. It won't fail for 2 types of
  317. * martian destinations: loopback destinations and destination
  318. * 0.0.0.0. In both cases the packet will be dropped because the
  319. * destination is the loopback device and not the bridge. */
  320. if (err != -EHOSTUNREACH || !in_dev || IN_DEV_FORWARD(in_dev))
  321. goto free_skb;
  322. rt = ip_route_output(net, iph->daddr, 0,
  323. RT_TOS(iph->tos), 0);
  324. if (!IS_ERR(rt)) {
  325. /* - Bridged-and-DNAT'ed traffic doesn't
  326. * require ip_forwarding. */
  327. if (rt->dst.dev == dev) {
  328. skb_dst_set(skb, &rt->dst);
  329. goto bridged_dnat;
  330. }
  331. ip_rt_put(rt);
  332. }
  333. free_skb:
  334. kfree_skb(skb);
  335. return 0;
  336. } else {
  337. if (skb_dst(skb)->dev == dev) {
  338. bridged_dnat:
  339. skb->dev = nf_bridge->physindev;
  340. nf_bridge_update_protocol(skb);
  341. nf_bridge_push_encap_header(skb);
  342. NF_HOOK_THRESH(NFPROTO_BRIDGE,
  343. NF_BR_PRE_ROUTING,
  344. net, sk, skb, skb->dev, NULL,
  345. br_nf_pre_routing_finish_bridge,
  346. 1);
  347. return 0;
  348. }
  349. ether_addr_copy(eth_hdr(skb)->h_dest, dev->dev_addr);
  350. skb->pkt_type = PACKET_HOST;
  351. }
  352. } else {
  353. rt = bridge_parent_rtable(nf_bridge->physindev);
  354. if (!rt) {
  355. kfree_skb(skb);
  356. return 0;
  357. }
  358. skb_dst_set_noref(skb, &rt->dst);
  359. }
  360. skb->dev = nf_bridge->physindev;
  361. nf_bridge_update_protocol(skb);
  362. nf_bridge_push_encap_header(skb);
  363. NF_HOOK_THRESH(NFPROTO_BRIDGE, NF_BR_PRE_ROUTING, net, sk, skb,
  364. skb->dev, NULL,
  365. br_handle_frame_finish, 1);
  366. return 0;
  367. }
  368. static struct net_device *brnf_get_logical_dev(struct sk_buff *skb, const struct net_device *dev)
  369. {
  370. struct net_device *vlan, *br;
  371. br = bridge_parent(dev);
  372. if (brnf_pass_vlan_indev == 0 || !skb_vlan_tag_present(skb))
  373. return br;
  374. vlan = __vlan_find_dev_deep_rcu(br, skb->vlan_proto,
  375. skb_vlan_tag_get(skb) & VLAN_VID_MASK);
  376. return vlan ? vlan : br;
  377. }
  378. /* Some common code for IPv4/IPv6 */
  379. struct net_device *setup_pre_routing(struct sk_buff *skb)
  380. {
  381. struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
  382. if (skb->pkt_type == PACKET_OTHERHOST) {
  383. skb->pkt_type = PACKET_HOST;
  384. nf_bridge->pkt_otherhost = true;
  385. }
  386. nf_bridge->in_prerouting = 1;
  387. nf_bridge->physindev = skb->dev;
  388. skb->dev = brnf_get_logical_dev(skb, skb->dev);
  389. if (skb->protocol == htons(ETH_P_8021Q))
  390. nf_bridge->orig_proto = BRNF_PROTO_8021Q;
  391. else if (skb->protocol == htons(ETH_P_PPP_SES))
  392. nf_bridge->orig_proto = BRNF_PROTO_PPPOE;
  393. /* Must drop socket now because of tproxy. */
  394. skb_orphan(skb);
  395. return skb->dev;
  396. }
  397. /* Direct IPv6 traffic to br_nf_pre_routing_ipv6.
  398. * Replicate the checks that IPv4 does on packet reception.
  399. * Set skb->dev to the bridge device (i.e. parent of the
  400. * receiving device) to make netfilter happy, the REDIRECT
  401. * target in particular. Save the original destination IP
  402. * address to be able to detect DNAT afterwards. */
  403. static unsigned int br_nf_pre_routing(void *priv,
  404. struct sk_buff *skb,
  405. const struct nf_hook_state *state)
  406. {
  407. struct nf_bridge_info *nf_bridge;
  408. struct net_bridge_port *p;
  409. struct net_bridge *br;
  410. __u32 len = nf_bridge_encap_header_len(skb);
  411. if (unlikely(!pskb_may_pull(skb, len)))
  412. return NF_DROP;
  413. p = br_port_get_rcu(state->in);
  414. if (p == NULL)
  415. return NF_DROP;
  416. br = p->br;
  417. if (IS_IPV6(skb) || IS_VLAN_IPV6(skb) || IS_PPPOE_IPV6(skb)) {
  418. if (!brnf_call_ip6tables && !br->nf_call_ip6tables)
  419. return NF_ACCEPT;
  420. nf_bridge_pull_encap_header_rcsum(skb);
  421. return br_nf_pre_routing_ipv6(priv, skb, state);
  422. }
  423. if (!brnf_call_iptables && !br->nf_call_iptables)
  424. return NF_ACCEPT;
  425. if (!IS_IP(skb) && !IS_VLAN_IP(skb) && !IS_PPPOE_IP(skb))
  426. return NF_ACCEPT;
  427. nf_bridge_pull_encap_header_rcsum(skb);
  428. if (br_validate_ipv4(state->net, skb))
  429. return NF_DROP;
  430. nf_bridge_put(skb->nf_bridge);
  431. if (!nf_bridge_alloc(skb))
  432. return NF_DROP;
  433. if (!setup_pre_routing(skb))
  434. return NF_DROP;
  435. nf_bridge = nf_bridge_info_get(skb);
  436. nf_bridge->ipv4_daddr = ip_hdr(skb)->daddr;
  437. skb->protocol = htons(ETH_P_IP);
  438. NF_HOOK(NFPROTO_IPV4, NF_INET_PRE_ROUTING, state->net, state->sk, skb,
  439. skb->dev, NULL,
  440. br_nf_pre_routing_finish);
  441. return NF_STOLEN;
  442. }
  443. /* PF_BRIDGE/FORWARD *************************************************/
  444. static int br_nf_forward_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
  445. {
  446. struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
  447. struct net_device *in;
  448. if (!IS_ARP(skb) && !IS_VLAN_ARP(skb)) {
  449. if (skb->protocol == htons(ETH_P_IP))
  450. nf_bridge->frag_max_size = IPCB(skb)->frag_max_size;
  451. if (skb->protocol == htons(ETH_P_IPV6))
  452. nf_bridge->frag_max_size = IP6CB(skb)->frag_max_size;
  453. in = nf_bridge->physindev;
  454. if (nf_bridge->pkt_otherhost) {
  455. skb->pkt_type = PACKET_OTHERHOST;
  456. nf_bridge->pkt_otherhost = false;
  457. }
  458. nf_bridge_update_protocol(skb);
  459. } else {
  460. in = *((struct net_device **)(skb->cb));
  461. }
  462. nf_bridge_push_encap_header(skb);
  463. NF_HOOK_THRESH(NFPROTO_BRIDGE, NF_BR_FORWARD, net, sk, skb,
  464. in, skb->dev, br_forward_finish, 1);
  465. return 0;
  466. }
  467. /* This is the 'purely bridged' case. For IP, we pass the packet to
  468. * netfilter with indev and outdev set to the bridge device,
  469. * but we are still able to filter on the 'real' indev/outdev
  470. * because of the physdev module. For ARP, indev and outdev are the
  471. * bridge ports. */
  472. static unsigned int br_nf_forward_ip(void *priv,
  473. struct sk_buff *skb,
  474. const struct nf_hook_state *state)
  475. {
  476. struct nf_bridge_info *nf_bridge;
  477. struct net_device *parent;
  478. u_int8_t pf;
  479. if (!skb->nf_bridge)
  480. return NF_ACCEPT;
  481. /* Need exclusive nf_bridge_info since we might have multiple
  482. * different physoutdevs. */
  483. if (!nf_bridge_unshare(skb))
  484. return NF_DROP;
  485. nf_bridge = nf_bridge_info_get(skb);
  486. if (!nf_bridge)
  487. return NF_DROP;
  488. parent = bridge_parent(state->out);
  489. if (!parent)
  490. return NF_DROP;
  491. if (IS_IP(skb) || IS_VLAN_IP(skb) || IS_PPPOE_IP(skb))
  492. pf = NFPROTO_IPV4;
  493. else if (IS_IPV6(skb) || IS_VLAN_IPV6(skb) || IS_PPPOE_IPV6(skb))
  494. pf = NFPROTO_IPV6;
  495. else
  496. return NF_ACCEPT;
  497. nf_bridge_pull_encap_header(skb);
  498. if (skb->pkt_type == PACKET_OTHERHOST) {
  499. skb->pkt_type = PACKET_HOST;
  500. nf_bridge->pkt_otherhost = true;
  501. }
  502. if (pf == NFPROTO_IPV4) {
  503. if (br_validate_ipv4(state->net, skb))
  504. return NF_DROP;
  505. IPCB(skb)->frag_max_size = nf_bridge->frag_max_size;
  506. }
  507. if (pf == NFPROTO_IPV6) {
  508. if (br_validate_ipv6(state->net, skb))
  509. return NF_DROP;
  510. IP6CB(skb)->frag_max_size = nf_bridge->frag_max_size;
  511. }
  512. nf_bridge->physoutdev = skb->dev;
  513. if (pf == NFPROTO_IPV4)
  514. skb->protocol = htons(ETH_P_IP);
  515. else
  516. skb->protocol = htons(ETH_P_IPV6);
  517. NF_HOOK(pf, NF_INET_FORWARD, state->net, NULL, skb,
  518. brnf_get_logical_dev(skb, state->in),
  519. parent, br_nf_forward_finish);
  520. return NF_STOLEN;
  521. }
  522. static unsigned int br_nf_forward_arp(void *priv,
  523. struct sk_buff *skb,
  524. const struct nf_hook_state *state)
  525. {
  526. struct net_bridge_port *p;
  527. struct net_bridge *br;
  528. struct net_device **d = (struct net_device **)(skb->cb);
  529. p = br_port_get_rcu(state->out);
  530. if (p == NULL)
  531. return NF_ACCEPT;
  532. br = p->br;
  533. if (!brnf_call_arptables && !br->nf_call_arptables)
  534. return NF_ACCEPT;
  535. if (!IS_ARP(skb)) {
  536. if (!IS_VLAN_ARP(skb))
  537. return NF_ACCEPT;
  538. nf_bridge_pull_encap_header(skb);
  539. }
  540. if (arp_hdr(skb)->ar_pln != 4) {
  541. if (IS_VLAN_ARP(skb))
  542. nf_bridge_push_encap_header(skb);
  543. return NF_ACCEPT;
  544. }
  545. *d = state->in;
  546. NF_HOOK(NFPROTO_ARP, NF_ARP_FORWARD, state->net, state->sk, skb,
  547. state->in, state->out, br_nf_forward_finish);
  548. return NF_STOLEN;
  549. }
  550. static int br_nf_push_frag_xmit(struct net *net, struct sock *sk, struct sk_buff *skb)
  551. {
  552. struct brnf_frag_data *data;
  553. int err;
  554. data = this_cpu_ptr(&brnf_frag_data_storage);
  555. err = skb_cow_head(skb, data->size);
  556. if (err) {
  557. kfree_skb(skb);
  558. return 0;
  559. }
  560. if (data->vlan_tci) {
  561. skb->vlan_tci = data->vlan_tci;
  562. skb->vlan_proto = data->vlan_proto;
  563. }
  564. skb_copy_to_linear_data_offset(skb, -data->size, data->mac, data->size);
  565. __skb_push(skb, data->encap_size);
  566. nf_bridge_info_free(skb);
  567. return br_dev_queue_push_xmit(net, sk, skb);
  568. }
  569. static int
  570. br_nf_ip_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,
  571. int (*output)(struct net *, struct sock *, struct sk_buff *))
  572. {
  573. unsigned int mtu = ip_skb_dst_mtu(skb);
  574. struct iphdr *iph = ip_hdr(skb);
  575. if (unlikely(((iph->frag_off & htons(IP_DF)) && !skb->ignore_df) ||
  576. (IPCB(skb)->frag_max_size &&
  577. IPCB(skb)->frag_max_size > mtu))) {
  578. IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
  579. kfree_skb(skb);
  580. return -EMSGSIZE;
  581. }
  582. return ip_do_fragment(net, sk, skb, output);
  583. }
  584. static unsigned int nf_bridge_mtu_reduction(const struct sk_buff *skb)
  585. {
  586. if (skb->nf_bridge->orig_proto == BRNF_PROTO_PPPOE)
  587. return PPPOE_SES_HLEN;
  588. return 0;
  589. }
  590. static int br_nf_dev_queue_xmit(struct net *net, struct sock *sk, struct sk_buff *skb)
  591. {
  592. struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
  593. unsigned int mtu, mtu_reserved;
  594. mtu_reserved = nf_bridge_mtu_reduction(skb);
  595. mtu = skb->dev->mtu;
  596. if (nf_bridge->frag_max_size && nf_bridge->frag_max_size < mtu)
  597. mtu = nf_bridge->frag_max_size;
  598. if (skb_is_gso(skb) || skb->len + mtu_reserved <= mtu) {
  599. nf_bridge_info_free(skb);
  600. return br_dev_queue_push_xmit(net, sk, skb);
  601. }
  602. /* This is wrong! We should preserve the original fragment
  603. * boundaries by preserving frag_list rather than refragmenting.
  604. */
  605. if (IS_ENABLED(CONFIG_NF_DEFRAG_IPV4) &&
  606. skb->protocol == htons(ETH_P_IP)) {
  607. struct brnf_frag_data *data;
  608. if (br_validate_ipv4(net, skb))
  609. goto drop;
  610. IPCB(skb)->frag_max_size = nf_bridge->frag_max_size;
  611. nf_bridge_update_protocol(skb);
  612. data = this_cpu_ptr(&brnf_frag_data_storage);
  613. data->vlan_tci = skb->vlan_tci;
  614. data->vlan_proto = skb->vlan_proto;
  615. data->encap_size = nf_bridge_encap_header_len(skb);
  616. data->size = ETH_HLEN + data->encap_size;
  617. skb_copy_from_linear_data_offset(skb, -data->size, data->mac,
  618. data->size);
  619. return br_nf_ip_fragment(net, sk, skb, br_nf_push_frag_xmit);
  620. }
  621. if (IS_ENABLED(CONFIG_NF_DEFRAG_IPV6) &&
  622. skb->protocol == htons(ETH_P_IPV6)) {
  623. const struct nf_ipv6_ops *v6ops = nf_get_ipv6_ops();
  624. struct brnf_frag_data *data;
  625. if (br_validate_ipv6(net, skb))
  626. goto drop;
  627. IP6CB(skb)->frag_max_size = nf_bridge->frag_max_size;
  628. nf_bridge_update_protocol(skb);
  629. data = this_cpu_ptr(&brnf_frag_data_storage);
  630. data->encap_size = nf_bridge_encap_header_len(skb);
  631. data->size = ETH_HLEN + data->encap_size;
  632. skb_copy_from_linear_data_offset(skb, -data->size, data->mac,
  633. data->size);
  634. if (v6ops)
  635. return v6ops->fragment(net, sk, skb, br_nf_push_frag_xmit);
  636. kfree_skb(skb);
  637. return -EMSGSIZE;
  638. }
  639. nf_bridge_info_free(skb);
  640. return br_dev_queue_push_xmit(net, sk, skb);
  641. drop:
  642. kfree_skb(skb);
  643. return 0;
  644. }
  645. /* PF_BRIDGE/POST_ROUTING ********************************************/
  646. static unsigned int br_nf_post_routing(void *priv,
  647. struct sk_buff *skb,
  648. const struct nf_hook_state *state)
  649. {
  650. struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
  651. struct net_device *realoutdev = bridge_parent(skb->dev);
  652. u_int8_t pf;
  653. /* if nf_bridge is set, but ->physoutdev is NULL, this packet came in
  654. * on a bridge, but was delivered locally and is now being routed:
  655. *
  656. * POST_ROUTING was already invoked from the ip stack.
  657. */
  658. if (!nf_bridge || !nf_bridge->physoutdev)
  659. return NF_ACCEPT;
  660. if (!realoutdev)
  661. return NF_DROP;
  662. if (IS_IP(skb) || IS_VLAN_IP(skb) || IS_PPPOE_IP(skb))
  663. pf = NFPROTO_IPV4;
  664. else if (IS_IPV6(skb) || IS_VLAN_IPV6(skb) || IS_PPPOE_IPV6(skb))
  665. pf = NFPROTO_IPV6;
  666. else
  667. return NF_ACCEPT;
  668. /* We assume any code from br_dev_queue_push_xmit onwards doesn't care
  669. * about the value of skb->pkt_type. */
  670. if (skb->pkt_type == PACKET_OTHERHOST) {
  671. skb->pkt_type = PACKET_HOST;
  672. nf_bridge->pkt_otherhost = true;
  673. }
  674. nf_bridge_pull_encap_header(skb);
  675. if (pf == NFPROTO_IPV4)
  676. skb->protocol = htons(ETH_P_IP);
  677. else
  678. skb->protocol = htons(ETH_P_IPV6);
  679. NF_HOOK(pf, NF_INET_POST_ROUTING, state->net, state->sk, skb,
  680. NULL, realoutdev,
  681. br_nf_dev_queue_xmit);
  682. return NF_STOLEN;
  683. }
  684. /* IP/SABOTAGE *****************************************************/
  685. /* Don't hand locally destined packets to PF_INET(6)/PRE_ROUTING
  686. * for the second time. */
  687. static unsigned int ip_sabotage_in(void *priv,
  688. struct sk_buff *skb,
  689. const struct nf_hook_state *state)
  690. {
  691. if (skb->nf_bridge && !skb->nf_bridge->in_prerouting)
  692. return NF_STOP;
  693. return NF_ACCEPT;
  694. }
  695. /* This is called when br_netfilter has called into iptables/netfilter,
  696. * and DNAT has taken place on a bridge-forwarded packet.
  697. *
  698. * neigh->output has created a new MAC header, with local br0 MAC
  699. * as saddr.
  700. *
  701. * This restores the original MAC saddr of the bridged packet
  702. * before invoking bridge forward logic to transmit the packet.
  703. */
  704. static void br_nf_pre_routing_finish_bridge_slow(struct sk_buff *skb)
  705. {
  706. struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
  707. skb_pull(skb, ETH_HLEN);
  708. nf_bridge->bridged_dnat = 0;
  709. BUILD_BUG_ON(sizeof(nf_bridge->neigh_header) != (ETH_HLEN - ETH_ALEN));
  710. skb_copy_to_linear_data_offset(skb, -(ETH_HLEN - ETH_ALEN),
  711. nf_bridge->neigh_header,
  712. ETH_HLEN - ETH_ALEN);
  713. skb->dev = nf_bridge->physindev;
  714. nf_bridge->physoutdev = NULL;
  715. br_handle_frame_finish(dev_net(skb->dev), NULL, skb);
  716. }
  717. static int br_nf_dev_xmit(struct sk_buff *skb)
  718. {
  719. if (skb->nf_bridge && skb->nf_bridge->bridged_dnat) {
  720. br_nf_pre_routing_finish_bridge_slow(skb);
  721. return 1;
  722. }
  723. return 0;
  724. }
  725. static const struct nf_br_ops br_ops = {
  726. .br_dev_xmit_hook = br_nf_dev_xmit,
  727. };
  728. /* For br_nf_post_routing, we need (prio = NF_BR_PRI_LAST), because
  729. * br_dev_queue_push_xmit is called afterwards */
  730. static struct nf_hook_ops br_nf_ops[] __read_mostly = {
  731. {
  732. .hook = br_nf_pre_routing,
  733. .pf = NFPROTO_BRIDGE,
  734. .hooknum = NF_BR_PRE_ROUTING,
  735. .priority = NF_BR_PRI_BRNF,
  736. },
  737. {
  738. .hook = br_nf_forward_ip,
  739. .pf = NFPROTO_BRIDGE,
  740. .hooknum = NF_BR_FORWARD,
  741. .priority = NF_BR_PRI_BRNF - 1,
  742. },
  743. {
  744. .hook = br_nf_forward_arp,
  745. .pf = NFPROTO_BRIDGE,
  746. .hooknum = NF_BR_FORWARD,
  747. .priority = NF_BR_PRI_BRNF,
  748. },
  749. {
  750. .hook = br_nf_post_routing,
  751. .pf = NFPROTO_BRIDGE,
  752. .hooknum = NF_BR_POST_ROUTING,
  753. .priority = NF_BR_PRI_LAST,
  754. },
  755. {
  756. .hook = ip_sabotage_in,
  757. .pf = NFPROTO_IPV4,
  758. .hooknum = NF_INET_PRE_ROUTING,
  759. .priority = NF_IP_PRI_FIRST,
  760. },
  761. {
  762. .hook = ip_sabotage_in,
  763. .pf = NFPROTO_IPV6,
  764. .hooknum = NF_INET_PRE_ROUTING,
  765. .priority = NF_IP6_PRI_FIRST,
  766. },
  767. };
  768. #ifdef CONFIG_SYSCTL
  769. static
  770. int brnf_sysctl_call_tables(struct ctl_table *ctl, int write,
  771. void __user *buffer, size_t *lenp, loff_t *ppos)
  772. {
  773. int ret;
  774. ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
  775. if (write && *(int *)(ctl->data))
  776. *(int *)(ctl->data) = 1;
  777. return ret;
  778. }
  779. static struct ctl_table brnf_table[] = {
  780. {
  781. .procname = "bridge-nf-call-arptables",
  782. .data = &brnf_call_arptables,
  783. .maxlen = sizeof(int),
  784. .mode = 0644,
  785. .proc_handler = brnf_sysctl_call_tables,
  786. },
  787. {
  788. .procname = "bridge-nf-call-iptables",
  789. .data = &brnf_call_iptables,
  790. .maxlen = sizeof(int),
  791. .mode = 0644,
  792. .proc_handler = brnf_sysctl_call_tables,
  793. },
  794. {
  795. .procname = "bridge-nf-call-ip6tables",
  796. .data = &brnf_call_ip6tables,
  797. .maxlen = sizeof(int),
  798. .mode = 0644,
  799. .proc_handler = brnf_sysctl_call_tables,
  800. },
  801. {
  802. .procname = "bridge-nf-filter-vlan-tagged",
  803. .data = &brnf_filter_vlan_tagged,
  804. .maxlen = sizeof(int),
  805. .mode = 0644,
  806. .proc_handler = brnf_sysctl_call_tables,
  807. },
  808. {
  809. .procname = "bridge-nf-filter-pppoe-tagged",
  810. .data = &brnf_filter_pppoe_tagged,
  811. .maxlen = sizeof(int),
  812. .mode = 0644,
  813. .proc_handler = brnf_sysctl_call_tables,
  814. },
  815. {
  816. .procname = "bridge-nf-pass-vlan-input-dev",
  817. .data = &brnf_pass_vlan_indev,
  818. .maxlen = sizeof(int),
  819. .mode = 0644,
  820. .proc_handler = brnf_sysctl_call_tables,
  821. },
  822. { }
  823. };
  824. #endif
  825. static int __init br_netfilter_init(void)
  826. {
  827. int ret;
  828. ret = nf_register_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
  829. if (ret < 0)
  830. return ret;
  831. #ifdef CONFIG_SYSCTL
  832. brnf_sysctl_header = register_net_sysctl(&init_net, "net/bridge", brnf_table);
  833. if (brnf_sysctl_header == NULL) {
  834. printk(KERN_WARNING
  835. "br_netfilter: can't register to sysctl.\n");
  836. nf_unregister_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
  837. return -ENOMEM;
  838. }
  839. #endif
  840. RCU_INIT_POINTER(nf_br_ops, &br_ops);
  841. printk(KERN_NOTICE "Bridge firewalling registered\n");
  842. return 0;
  843. }
  844. static void __exit br_netfilter_fini(void)
  845. {
  846. RCU_INIT_POINTER(nf_br_ops, NULL);
  847. nf_unregister_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
  848. #ifdef CONFIG_SYSCTL
  849. unregister_net_sysctl_table(brnf_sysctl_header);
  850. #endif
  851. }
  852. module_init(br_netfilter_init);
  853. module_exit(br_netfilter_fini);
  854. MODULE_LICENSE("GPL");
  855. MODULE_AUTHOR("Lennert Buytenhek <buytenh@gnu.org>");
  856. MODULE_AUTHOR("Bart De Schuymer <bdschuym@pandora.be>");
  857. MODULE_DESCRIPTION("Linux ethernet netfilter firewall bridge");